- Home /
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) ;)
Answer by GameDevBryant · Jun 28, 2013 at 04:22 PM
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.
Answer by Weirdo-Studios · Jun 28, 2013 at 04:29 PM
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");
I think you also forgot to refernece what the animaitonclip actually was in the inspector
Answer by Digital-Phantom · Jun 29, 2013 at 06:19 PM
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 ???
I'm not entirely sure if you can set the time to go in reverse, but you can try playing with the ping pong wrap mode to see if you can get it that way:
That makes it play forwards then backwards by default.
Your answer
Follow this Question
Related Questions
Reference to Object error but script works fine 1 Answer
Destroy trigger so animation only plays once. 1 Answer
Help with Animator has not been initialized error 5 Answers
animation and sound on collide 1 Answer
Trigger animtaion when hit 1 Answer