Animation Bug?

Hello, i have an idle animation, an shooting animation, and a reload animation. But it’s really buggy! My Idle works great but when ever i try to shot or reload the gun just twitches (barely moves). Whats the problem here? Please help me!
~carlqwe

Code(javaScript):

#pragma strict
     
 var ammo : int;
 var cooldown : float;
     
 function Start ()
 {
     GetComponent.<Animation>().Play("Mp5_Idle");
     ammo = 30;
 }
     
 function Update ()
 {
     if(Input.GetMouseButton(0) && 0 <= ammo && cooldown <= Time.time)
     {
         cooldown=Time.time+0.1f;
         SprayMP5 ();
     }
     else
     {
         StopMp5();
     }
 
     if(Input.GetKeyDown(KeyCode.R))
     {
         GetComponent.<Animation>().Stop("Mp5_Idle");
         GetComponent.<Animation>().Play("Mp5_Reload");
         StopReload();
     }
 }
     
 function SprayMP5 ()
 {
     GetComponent.<Animation>().Play("Mp5_Shot");
     GetComponent.<Animation>().Stop("Mp5_Idle");
     ammo -= 1;
 }
     
 function StopMp5()
 {
     GetComponent.<Animation>().Stop("Mp5_Shot");
     GetComponent.<Animation>().Play("Mp5_Idle");
 }
     
 function OnGUI ()
 {
     GUI.Label(new Rect(30, 80, 150,50), ammo.ToString());
 }
 
 function StopReload ()
 {
     yield WaitForSeconds(1.2);
     GetComponent.<Animation>().Stop("Mp5_Reload");
     GetComponent.<Animation>().Play("Mp5_Idle");
     ammo = 30;
 }

Hi, I think I see the problem. When you press “R” to reload, every single frame that key is down is starting another instance of the StopReload() coroutine. I had this issue a lot and I fell in love with using handy bools to prevent multiple calls. It should work if it’s written like this:

function Update()
{  
    if(Input.GetKeyDown(KeyCode.R))
    {
        StopReload();
    }
}
    
bool canStopReload = true;
    
function StopReload ()
{
    if (canStopReload == true)
    {
        canStopReload = false;
        GetComponent.<Animation>().Stop("Mp5_Idle");
        GetComponent.<Animation>().Play("Mp5_Reload");
        yield WaitForSeconds(1.2);
        GetComponent.<Animation>().Stop("Mp5_Reload");
        GetComponent.<Animation>().Play("Mp5_Idle");
        ammo = 30;
        canStopReload = true;
    }
}

I moved the “Stop Idle” and “Play Reload” into the StopReload function, as they should be part of the function and not the keypress if you want to avoid weird issues down the road (it’s best to keep things self-contained). Be aware that I use C#, so I apologize ahead of time for any syntax mistakes.