- Home /
Unity animations never play, only pause game?!!?!
Hi. Im having this REALLY annoying problem with my unity engine. Ive done some animations, and no matter what way i try to activate them, it never works. The script comes up with no errors, it just pauses the game debug thing. If i unpause it, its fine!. SO ANNOYING!. Ive created new projects and they do not work either. I've tried all methods that are online, and every step of you tube vids and i just cannot get these animations to activate!!!! When i build and run, it shows the error "Object reference not set to an instance of the object" and doesn't pause the game, it just continued as if it never happened? This is basically the code i use each time below. I use the variable of the animation to change things, even without it it doesn't work. Its currently really limiting my projects! Any help would be greatly appreciated! Thanks
var anim : AnimationClip;
function Update () {
if( Input.GetKeyDown("r")){
animation.Play("anim"); }
}
Answer by kacyesp · Sep 02, 2014 at 12:00 PM
Make sure you attach the animation to the game object in the inspector (Add Component -> Miscellaneous -> Animation). Once you've attached the Animation component, increase the size to however many animations you have. Then drag your Animation clips into the empty slots. I posted a screenshot below of what the Animation Component should look like.
function Update () {
if( Input.GetKeyDown("r"))
animation.Play("nameOfAnimation");
}
I increased this Animation component to have a size of 4, meaning I can add 3 more animations to it since I've already added 1.
Answer by Gizmoi · Sep 02, 2014 at 11:06 AM
Sounds like 'animation' isn't being set properly, which is causing a null reference when you try to access it.
I imagine you also have the 'Error Pause' option checked on the debug console, which will cause the Editor to pause when an exception is thrown.
You should make sure that animation is assigned correctly. I suspect you actually meant to use the declared member variable 'anim'. If this is so then your code should be:
var anim : AnimationClip;
function Update()
{
if (Input.GetKeyDown("r"))
{
if (animation != null)
{
animation.Play("anim");
}
}
}
Hello. I tired that but it did not work, however that doesnt mean that it isnt progress. Now when i build and run it i get the error "$$anonymous$$issing$$anonymous$$ethodExeption.UnityEngine.AnimationClip.Play" $$anonymous$$aybe this is a easier problem to solve? Thanks
Ah yes, I was mistaken. I didn't realise your original code was accessing the 'animation' property from $$anonymous$$onoBehaviour.
Your original code was correct. However you should add an Animation component to the object that has this script on it.
You should probably also add a null reference check to stop the error happening. I'll edit the answer to show this.
Answer by Quadriladder · Sep 02, 2014 at 12:42 PM
Thankyou so much for the great responses. I ended up adding the animations like above, then removing the variable and works great. Thanks everyone!
The procedure is you got to mark the respond which helped you the most!
You should upvote anything that was helpful to you, whether it was actual answers or comments. And you should mark whichever answer helped you the most as the accepted answer.