Weid Null reference exception

Hi there,

I am using this script from Peneolpe tutorial to get additionnal life when the player collide with the game object it is attached to :

ParticlePickup.js

var emitter : ParticleEmitter;
var index : int;
var collectedParticle : GameObject;
private var sk : ChronoScoreKeeperKBNew00;
// OnTriggerEnter is called whenever a Collider hits this GameObject's collider
function OnTriggerEnter(other : Collider)
{
	sk = other.GetComponent( ChronoScoreKeeperKBNew00 );
	sk.Pickup( this );
}


// Collected is called when the player picks up this item.
function Collected()
{
	// Spawn particles where the orb was collected
	Instantiate( collectedParticle, transform.position, Quaternion.identity );
	
	// Scale the particle down, so it is no longer visible
 	var particles : Particle[] = emitter.particles;	 	
 	particles[ index ].size = 0;	 	
 	emitter.particles = particles;
	
	// Destroy the collider for this orb
	Destroy( gameObject );
}

and I have this Function Pickup within a script (ChronoScoreKeeperKBNew00) attached to the player :

public function Pickup( pickup : ParticlePickup )
{
	if ( playerInfo.health < playerInfo.maxHealth )
	{
	 	playerInfo.health++;
	 	UpdateHealthGui();
	 			
	 	pickup.Collected();	
		PlayAudioClip( pickupSound, pickup.transform.position, 1.0 );
	}
	else
	{
		var warning : GameObject = Instantiate( guiMessage );
		warning.guiText.text = "You already have a six pack life";
		Destroy(warning, 2);
	}
		
}

What puzzles me is that everything works fine, I get the additionnal life when my player collides with this GO, I get the sound, I get the txt message if I already have the maxHealth, but I get a" null reference exception : Object reference not set to an instance of an object" which points to ParticlePickup script on the following line :

sk.Pickup( this );

Am I missing something here ??
thks in advance for your help .

If you’ve got something like this:-

sk = other.GetComponent( ChronoScoreKeeperKBNew00 );
sk.Pickup( this );

…and there is a null reference reported on the second line, it probably means the result returned by the GetComponent call was null. You can check for sure by adding an explicit test:-

if (sk == null) {
    print("sk is null");
}

If it turns out that sk is null, then the first thing to check is that there actually is a ChronoScoreKeeperKBNew00 script attached to the “other” collider object. It’s likely that the object is colliding with something unexpected.

Thanks Andeee, you helped me again find the problem. There was obviously another object colliding so I am now more specific with this code and everything’s fine.

function OnTriggerEnter(other : Collider)
{
	if(other.collider.tag == "Player"){			
	sk = other.GetComponent( ChronoScoreKeeperKBNew00 );
	sk.Pickup( this );
	}	

}