- Home /
if (animation ["idle"].enabled == true){ gives a NullReferenceExeption?
I don't know what I need to do to get rid of this. I did try var enabled :bool as described on the docs pages but that caused a problem. This is where I was looking http://answers.unity3d.com/questions/14599/check-if-a-spesific-animation-is-playing.html. As you can see I am looking to see if an animation is playing. Or is there an other way to do this?
the ":bool" denotes the 'Type' of the function. You don't need to include it in your call.
Erisat's answer is correct for Legacy system.
Have used that but I don't want it to work on a mouse click I just want to check that a specific animation is playing as I am animating a scene rather than using it as part of something interactive. Also I can't use legacy as I am animating a character.
I have been but now I am using legacy animation. What I want to do is to say that when a specific animation is playing, play a certain sound. I am working with 2 animations and 2 sounds on one character. I can get both animations to play one after the other but not the sounds they seem to trigger at the same time.
#pragma strict
//private var state = 0;
var idle : GameObject;
var scratch : GameObject;
var idleSound : AudioClip;
var scratchSound : AudioClip;
var enabled = true;
function Start () {
animation.Play ();
if (animation.isPlaying);
//audio.Play(88200);
audio.PlayOneShot(idleSound);
//yield WaitForSeconds(1);
//if (!animation.isPlaying);
//animation.Play ("$$anonymous$$ouseScratch");
////if (animation ["$$anonymous$$ouseScratch"].enabled == true){
//if (scratch.animation.isPlaying);
//
// audio.PlayOneShot(scratchSound);
}
function Update () {
//if (state == 0){
// state = 1;
// return;
//}
}
Answer by Bunny83 · Jun 20, 2014 at 09:01 AM
You have to understand what that error actually means. A Null-Reference-Exception happens when you use a reference to an object to do something with it, but that reference is null. If a reference is null that means that object you want to access doesn't exists.
In your code there are two possible candidates:
The animation component itself
or there is no AnimationState called "idle" in that animation component
So you either have this script on a GaemObject that doesn't have an Animation componentn attached, or that animation component doesn't have an animation called "idle".
Keep in mind that the Animation component need to be on the same OameObject. If it's on a child GameObject you can't access it that way. You would need to either get a reference to the correct child object or use GetComponentInChildren to get the Animation component.
Your answer
Follow this Question
Related Questions
Enabling/Disabling a slider from script 0 Answers
this == null 1 Answer
Weird NullReferenceException 2 Answers
Arraylist to GameObject[] array issue. 1 Answer
[Roll-a-ball] Changing UI text (NRE) 0 Answers