NullReferenceException problem cant find where

Hi everyone!

Im going crazy with this script. My goal is to get a script that, when I press and hold the mouse button, it charges an IceBall (incrementing it’s size with transform.localScale), and when the mouse button is released, it shots the ball. Im trying to comunitate with the Stats script, wich its attached to the player, but I constantly get the NullReferenceException problem. Here are the codes:

EsferaHielo.js:

var cannonball : Transform;
var speed      : float = 50; // speed is power per second
var aumento    : float = 0.0001; // multiplicador de aumento de tamaño
var powermax   : float = 500;
var powermin   : float = 100;

private var projectile;
private var nivelHielo = Estadisticas.lvl_hielo;
var tamanio = nivelHielo * 1.5;
var maxTamanio = Vector3(tamanio, tamanio, tamanio);

//function Start(){
//}

function Update() 
{    
    if (Input.GetButtonDown("Fire1")) 
        projectile = Instantiate(cannonball,
                                     transform.position,
                                     transform.rotation);
        StartCoroutine("Cook");
}
 
// This is a co-routine. 
// I used the term "to cook a grenade" as popular in many action games.
function Cook() 
{
    var power : float = powermin;
    
    while (Input.GetButton("Fire1"))
    {
        var move = speed * Time.deltaTime;
        power = Mathf.MoveTowards(power, powermax, move);
        var size: float = aumento * nivelHielo;
        projectile.localScale  += Vector3(size, size, size);
        
    }
//    if (projectile.localScale > maxTamanio)
//    	{
//    		projectile.localScale = maxTamanio;
//    	}
 
    Shoot(power);
}
 
function Shoot(power : float)
{

        projectile.rigidbody.AddForce(transform.forward * power);
        Physics.IgnoreCollision(projectile.collider, collider);
}

Estadisticas.js

//#pragma strict

static var vida : int = 100;
static var maxVida : int = 100;
var g_vida : GUIText;
var g_NombreYNobleza : GUIText;
var g_niveles : GUIText;

static var nivel_pj : int = 1;
static var lvl_hielo : int = 1;
static var lvl_fuego : int = 1;
static var lvl_levitar : int = 1;
static var lvl_volar : int = 1;
var nombre = 'Polomeo';
var tituloNobleza = 'Aprendiz';
var experienciaTotal : int = 0; // cada vez que suma XP a un hechizo, tambien se suma aca.
var nivel : int = 0;

private var xp_hielo : int = 0;
private var maxXp_hielo : int = 100;
private var xp_fuego : int = 0;
private var maxXp_fuego : int = 100;
private var xp_levitar : int = 0;
private var maxXp_levitar : int = 100;
private var xp_volar : int = 0;
private var maxXp_volar : int = 100;

private var experienciaSigLvl = 400;

private var titulosDeNobleza = ['Aprendiz', 'Duque', 'Lord', 'Master', 'Capo'];

function Start(){
	
	RegenerarVida();
	//RegenerarMana();

}

function Update(){

	g_niveles.text = "XP TOTAL =" + experienciaTotal + " - Hielo: " + xp_hielo + " Lvl Hielo: " + lvl_hielo; 
	g_vida.text = "Vida: " + vida + " / " + maxVida;
	g_NombreYNobleza.text = tituloNobleza + " " + nombre + " Nivel " + nivel_pj;
	
	
	if( vida > maxVida)
		{
			vida = maxVida;
		}
	
	if( vida < 0)
		{
			Debug.Log('Muerto');
			vida = 0;
		}
		
	if (Input.GetKeyDown('r'))
		{
			vida -= 20;
		}
	if (experienciaTotal > experienciaSigLvl)
		{
			SubirNivel();
		}
	
	if (Input.GetKeyDown('e'))
		{
			xp_hielo += 10;
			experienciaTotal += 10;
		}
	
	if (xp_hielo >= maxXp_hielo)
		{
			SubirNivelHielo();
		}
		

}

function RegenerarVida(){
	
	for(i=1;i>0;i++) 
	{
 		yield WaitForSeconds(0.5); // lo que espera es lo que tarda en subir un punto la vida. A mas nivel, deberia subir mas rapido
 
		if(vida < maxVida) 
		{
			vida++;
 		} 
	}
}

function SubirNivel(){
	
	experienciaSigLvl += 400 * nivel_pj;
	nivel_pj++;
	tituloNobleza = titulosDeNobleza[nivel_pj];
	maxVida += 50 * nivel_pj;
	
}

function SubirNivelHielo(){
	
	xp_hielo = 0;
	lvl_hielo++;
	maxXp_hielo += 50 * lvl_hielo;
}

Thanks!

A possible cause for your problems is the projectile variable being untyped: the compiler doesn’t know its type, thus using Transform properties like localScale or rigidbody generates runtime errors. Declare it as Transform and probably the errors will disappear:

private var projectile: Transform;

NOTE: The assignment nivelHielo = Estadisticas.lvl_hielo; will happen only once when the object to which EsferaHielo is attached is created - is this ok?

Nevermind people!

I found the answer.

I rewrite the code from scratch, using an example that I found in the forums.

This is the code working. The only thing left is to set and tweek the variables.

var cannonball : Transform;
var speed      : float = 500; // speed is power per second
var powermax   : float = 5000;
var powermin   : float = 100;
var autoThrow  : boolean = true;
private var projectile;
 
function Update() 
{    
    if (Input.GetButtonDown("Fire1")) 
        StartCoroutine("Cook");
}
 
// This is a co-routine. 
// I used the term "to cook a grenade" as popular in many action games.
function Cook() 
{
    var power : float = powermin;
    projectile = Instantiate(cannonball,
                                     transform.position,
                                     transform.rotation);
 
    while (Input.GetButton("Fire1"))
    {
        var move = speed * Time.deltaTime;
        power = Mathf.MoveTowards(power, powermax, move);
        var size : float = 2.0;
        var maxTamanio : Vector3 = Vector3(2.0, 2.0, 2.0);
        projectile.localScale += Vector3(0.1,0.1,0.1);
        if (projectile.localScale.magnitude >= maxTamanio.magnitude)
        	{projectile.localScale = Vector3(2.0, 2.0, 2.0);}
        
 
        if (autoThrow && power == powermax) 
            break;
        else 
            yield;
    }
 
    Shoot(power);
}
 
function Shoot(power : float)
{
         projectile.rigidbody.AddForce(transform.forward * power);
        //Physics.IgnoreCollision(projectile.collider, collider);
}

Thanks a lot for all your help!