Why won't this code play my animation?

I have a script that I want to make an animation play when you press “Fire1”, but it just won’t work. Here’s the script…

var sound : AudioClip;

function Update () {

    if(Input.GetButtonDown("Fire1"))
    {
        AudioSource.PlayClipAtPoint(sound, transform.position);
        GetComponent.<Animation>().Play();
    }
    }

I think I have the animation set up right. Is this right?
2613741--183342--unity issue.png

Try opening the swing animation to Inspector, and setting debug mode on. Set Legacy mode on.

Does it play the sound?

Also legacy animation system is old i guess no longer supported. I wouldn’t recommend building a game around it.

The ‘swing’ animation needs to exist in the Animations list for it to play. It’s a weird quirk with the Animation system - it only plays animations that are in the Animations list even if you properly reference them.

The Animation component is the legacy way of doing things. The Animator component is the new way. You shouldn’t add both components. I suggest you only use an Animator component - the animations are set up by adding states to the controller.

It does play the sound, and I’m not sure what legacy animation is. I just started animating about 2 days ago

It still didn’t work with just the animator.

No, I didn’t mean to imply that it would. You are referencing the Animation component in your script which, of course, won’t work without an Animation component on the game object :slight_smile:
As I said, if you want to use the Animator component you need to set up the state machine in a meaningful way.

If you need general information about the animation system, the manual probably has all the information you need.

Also, if you still want to use the legacy animation system it seems like @RedVonix has already answered the question.

It still doesn’t work. I have no idea what to do.