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”);
}

}

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");
    }
  }
}

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"); 
    
}

31902-animationcomponent.png

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.

Thankyou so much for the great responses. I ended up adding the animations like above, then removing the variable and works great. Thanks everyone!