nullReferenceException error

I am getting this error when I click play: NullReferenceException

This is the code:

animation[takeOutAnim].speed = (animation[takeOutAnim].clip.length/.6);

animation.Play(takeOutAnim);

2 Answers

2

The exception means you are trying to do something with a null object. Your two lines of code would produce that error if `animation` is null, if `animation[takeOutAnim]` is null, or if `animation[takeOutAnim].clip` is null.

You can add some error-checking code to find out which is the problem, like so ...

if (animation == null)
{
    Debug.Log("animation is null!", gameObject);
}
if (animation[takeOutAnim] == null)
{
    Debug.Log("animation does not contain takeOutAnim!", gameObject);
}
if (animation[takeOutAnim].clip == null)
{
    Debug.Log("animation clip is null!", gameObject);
}

animation[takeOutAnim].speed = (animation[takeOutAnim].clip.length/.6);

animation.Play(takeOutAnim);

There's your problem then. What is the value of takeOutAnim? Either the string is wrong, or the animation doesn't contain the clip you think it does.

–

Where do you declare takeOutAnim? (Maybe you mean "takeOutAnim", i.e. the string itself?)

–

I tried to add it but then another error appeared telling that:

'Length' is not a member of 'UnityEngine.Animation'

Oops, my bad -- I didn't look at how animations are indexed. I just edited my answer to fix this. Oh, and please make this a comment, not another answer.

–