- Home /
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);
Answer by yoyo · Mar 17, 2011 at 06:29 PM
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);
After I added it this error appeared:
animation does not contain takeOutAnim! UnityEngine.Debug:Log(Object, Object)
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? ($$anonymous$$aybe you mean "takeOutAnim", i.e. the string itself?)
Answer by Michael 20 · Mar 17, 2011 at 07:22 PM
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.
Your answer
