- Home /
Animation Error "Null reference exception"?
I am building a FPS (not the tutorial) and I built a series of animations for a gun model using Maya (a .mb file). I have imported the animations, and written a basic script that runs the bolt slide animation when you click the mouse (and fire the weapon) here is the script:
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
animation["Element 0"].wrapMode = WrapMode.Once;
animation.Stop();
}
}
however, when I run this script, I get a "Null reference exception" message in the debug window, what is the error?
Thanks
Which line exactly are you getting the NullReferenceException on?
Line 5: animation["Element 0"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
Answer by Slim · Feb 18, 2010 at 08:41 PM
Wow, I just used the animation.Play command and it works fine, case solved. Now, I wonder what the difference between wrapMode and play is...
Answer by jashan · Feb 17, 2010 at 03:53 PM
Most likely there is no animation by the name of "Element 0". Or, the game object that you have attached this script to doesn't have an animation component attached to it at all.
Well, "element 0" is the first element of the animation component of gameObject $$anonymous$$1.Reload (my weapon) $$anonymous$$1.Reload is also the true name of the animation...
"element 0" or "Element 0" - in your code, you have "Element 0" (upper-case E), and if it's actually called "element 0" (lower-case e) that won't work as keys in dictionaries are case-sensitive. So that might be it. I'm not perfectly sure I understand you correctly, though - if the name of the animation is "$$anonymous$$1.Reload", it would have to be animation[$$anonymous$$1.Reload"] ins$$anonymous$$d. You should also be able to access the animation via animation[0] though (that would be the first animation component).
I do not believe it is the name that is the issue, I simplified the game quite a bit and made it so its supposed to only do one thing when the mouse is clicked, run "$$anonymous$$1.Reload" But even with this simple program, while I don't get any error message, it simply won't play the animation. When I check the play automatically box in the animation component of the m1 gameObject, the animation plays, which proves that the animation IS there...
Your answer
![](https://koobas.hobune.stream/wayback/20220612074520im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Animation script with is.grounded help 0 Answers
change animation name in inspector 2 Answers
Animation component in Unity 4.3 doesn't work? 2 Answers
Character controller problem? 2 Answers