How to get the animation to play through?

Right now, I am trying to get an animation to play through all the way but it doesn't. The code I am using is:

function Update ()
{
   if(Input.GetKeyDown("joystick button 2"))
   {
      animation.Play("HiroXAttack1"); // name of the animation clip 
   }
}

I have to press the button repeatedly to play the entire animation through.

I tried a different approach and did:

var isAttacking = false;
function Update ()
{
    if(Input.GetKeyDown("joystick button 2"))
    {
        isAttacking = true;
    }

    if(isAttacking)
    {
        animation.Play("HiroXAttack1");
    }

    if(Input.anyKey == false)
    {
        isAttacking = false;
        animation.Stop();
    }
}

What this does is it plays the animation, and loops through but doesn't stop. I was looking through the animation reference page but I couldn't see anything that would help my current situation. So my question is how do I get the animation to play through once, when I press the attack button, and then not play the animation continuously? Any example code would be greatly appreciated, Thank you.

Your first code snippet should work, provided you have set your animation mode to Once, or Clamp, or ClampForever.

function Update ()
{
   if(Input.GetKeyDown("joystick button 2"))
   {
      animation.wrapMode = WrapMode.Once;
      animation.Play("HiroXAttack1"); // name of the animation clip 
   }
}

Are you sure there's no other code affecting your animation?

The first code snippet should work fine. It sounds like there is some other code that is affecting the playback.

Note that the default behavior of Animation.Play is to stop all other animations on the same layer. You say that "I don't have any other functions that handle this animation." but even if you have other functions that handle other animations, it could still very likely be what is stopping this animation from playing all the way through.

The easiest way to solve this particular case is to put the HiroXAttack1 animation on a higher layer. This will make sure that it keeps playing, even if other animations on lower layers are playing at the same time. More info on animation layers here.

There is also a solution if you want the animations to be on the same layer. To play a given animation without stopping other animations on the same layer, you need to not use Animation.Play but instead do:

animation["MyOtherAnimation"].enabled = true;
animation["MyOtherAnimation"].weight = 1;
animation["MyOtherAnimation"].time = 0;

Note that the final result will be a blend of all currently playing animations though, so only have multiple animations on the same layer play at the same time if you know what you're doing. :)

Thanks everyone for all your help. Once I put the animation on a higher layer, it played completely and I used the animation.CrossFade to sort of blend the two animations together. Thanks again, you guys are great.

"GetButtonDown" other than "GetKeyDown" should do it

I think the default animation looping mode is set to Wrap. I think there is a one shot option that you would set explicitly.

Also, it looks like the above code would require you to hold the button down for the duration of the clip?

That's what I'm trying to figure out. I have this above the update function:

function Start ()
{
    animation["HiroXAttack1"].wrapMode = WrapMode.Once;
}

So I was wondering if I'm missing anything else. When I press the attack button, it only plays the first part of the animation but doesn't do the complete animation. I don't have any other functions that handle this animation.