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 Check if a spesific animation is playing - Unity Answers. As you can see I am looking to see if an animation is playing. Or is there an other way to do this?

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.