Why am I getting this error ? (Animation based)

Why am I getting this error message? What do they mean by object reference not set? Ive followed a tutorial to the letter and cant see what Im doing wrong ?

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type

Script Im using -

var doorClip : AnimationClip;


function OnTriggerEnter (player : Collider)
{
	if(player.tag == "Player")
	GameObject.Find("Door").Animation.Play("Door_Open");
}

The animation Im sure is correct (if left on auto it plays fine)

Im getting this error at the exact moment my player character enters the doors box collider.

I’ll guess its probably something obvious, but Im still kinda new to unity and cant figure it out…lol

Help appreciated please guys(or ladies) :wink:

A null reference exception means you are trying to access something that is set to null (aka not set at all). Based on this code snippet it could be one of two things: your game object or your animation component.

Try separating it out into something like this:

GameObject door = GameObject.Find("Door");
door.animation.clip = doorClip;
door.animation.Play();

That way, if you still get the error you will no which part is null based on the line the error sends you to.

[Edit] If you are doing it in OnTriggerEnter(), you don’t have to call GameObject.Find or assign the clip each time. That can be done in Start() or somewhere that only gets called once (making the variable ‘door’ in my example a class member variable.

It looks to me like you don’t need the variable because you don’t reference it in your script you just link straight to the animation when you say:

GameObject.Find(“Door”).Animation.Play(“Door_Open”);

Thanks guys, sorted now (well phase one is anyway)

#pragma strict

//Puts Drag n Drop variable slot into Script
var doorClip : AnimationClip;

function Start () {

}

function OnTriggerEnter () {

	animation.Play("SlideOpen"); // Sets name of Animation clip to be played

}

This did the trick, along with your suggestions.

Side question - Do I have to make another animation for the door close? or is there a way to just play the DoorOpen animation in reverse with an OnTriggerExit function ???