Preventing reload from being spammed.

As simple as the title says, Im having trouble to figure out how I would make the reload function unable to be spammed by the user (pressed mutliple times)

If i press R 4 times quickly, it will reload 4 times while running through 1 reload animation, which is unrealistic, so I need to fix that.

I was proposing something like if function reload is playing, then wait 5 seconds till you can reload again.

I want to stay away from animations, because my animation script is a child of the machinegun script, and is not attached to it, so accessing the animation is impossible unless its on the same game object.

I understand the algorithm for this but I just cant seem to find a way to script it.

Heres my machinegun script:

var range = 200.0; 
var fireRate = 0.05; 
var force = 10.0; 
var damage = 5.0; 
var bulletsPerClip = 100; 
var reloadTime = 5; 
private var hitParticles : ParticleEmitter; 
var muzzleFlash : Renderer; 
static var bulletsLeft : int = 0; 
static var clipsLeft : int = 2; 
private var nextFireTime = 0.0; 
private var m_LastFrameShot = -1; 

function Start () { 

   hitParticles = GetComponentInChildren(ParticleEmitter); 
   // We don't want to emit particles all the time, only when we hit something. 
   if (hitParticles) 
      hitParticles.emit = false; 
   bulletsLeft = bulletsPerClip; 
} 

function LateUpdate() { 
   if(Input.GetButtonDown("Reload")  clipsLeft >= 1) 
   ButtonReload(); 
	 
   if (muzzleFlash) { 
      // We shot this frame, enable the muzzle flash 
      if (m_LastFrameShot == Time.frameCount) { 
         muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward); 
         muzzleFlash.enabled = true; 

         if (audio) { 
            if (!audio.isPlaying) 
               audio.Play(); 
            audio.loop = true; 
         } 
      } else { 
      // We didn't, disable the muzzle flash 
         muzzleFlash.enabled = false; 
         enabled = true; //<---- enable true if you want to reload manually, but with no reload time. 
          
         // Play sound 
         if (audio) 
         { 
            audio.loop = false; 
         } 
      } 
   } 
} 
function Fire () { 
   if (bulletsLeft == 0  clipsLeft >=1) { 
     Reload(); 
     return; 
    } 
    
   // If there is more than one bullet between the last and this frame, Reset the nextFireTime 
   if (Time.time - fireRate > nextFireTime) 
      nextFireTime = Time.time - Time.deltaTime; 
    
   // Keep firing until we used up the fire time 
   while( nextFireTime < Time.time  bulletsLeft != 0) { 
      FireOneShot(); 
      nextFireTime += fireRate; 
   } 
} 
function FireOneShot () { 
   var direction = transform.TransformDirection(Vector3.forward); 
   var hit : RaycastHit; 
    
   // Did we hit anything? 
   if (Physics.Raycast (transform.position, direction, hit, range)) { 
      // Apply a force to the rigidbody we hit 
      if (hit.rigidbody) 
         hit.rigidbody.AddForceAtPosition(force * direction, hit.point); 
        
      // Place the particle system for spawing out of place where we hit the surface! 
      // And spawn a couple of particles 
      if (hitParticles) { 
         hitParticles.transform.position = hit.point; 
         hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); 
         hitParticles.Emit(); 
      } 

      // Send a damage message to the hit object          
      hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); 
   } 
//decrease bullets by 1. 
   bulletsLeft--; 
   // Register that we shot this frame, so that the LateUpdate function enabled the muzzleflash renderer for one frame 
   m_LastFrameShot = Time.frameCount; 
   enabled = true; 
    
   //gun reload in time 
   if (bulletsLeft == 0  clipsLeft >=1) { 
     Reload(); 
    }
} 

function Reload (){ 
SendMessageUpwards("setEnableWeaponSwitch", false); 
//audio.PlayOneShot(reloadSound, 1.0 / audio.volume); 

//    Wait for reload time first - then add more bullets! 
yield WaitForSeconds(reloadTime); 
    
   // We have a clip left reload 
 if (clipsLeft > 0 ) { 

   clipsLeft--; 
     bulletsLeft = bulletsPerClip; 
   }
SendMessageUpwards("setEnableWeaponSwitch", true); 
} 

//function for manual reloading. 
function ButtonReload (){ 

SendMessageUpwards("setEnableWeaponSwitch", false); 
//audio.PlayOneShot(reloadSound, 1.0 / audio.volume); 

//    Wait for reload time first - then add more bullets! 
yield WaitForSeconds(reloadTime); 
    
   // We have a clip left reload 
 if (clipsLeft > 0 ) { 

   clipsLeft--; 
     bulletsLeft = bulletsPerClip; 
   }
SendMessageUpwards("setEnableWeaponSwitch", true); 
} 


function GetBulletsLeft () { 
   return bulletsLeft; 
} 

function GetClipsLeft () { 
return clipsLeft; 
}

Bump. Anyone with any solution?

You could set up a global float variable that is your reload start time. In your start function set it to -reloadTime and then change your Reload function to this.

function Reload (){
   if (Time.time - reloadStartTime < reloadTime)
      return;
   reloadStartTime = Time.time;
   SendMessageUpwards("setEnableWeaponSwitch", false); 
   //audio.PlayOneShot(reloadSound, 1.0 / audio.volume); 

   //    Wait for reload time first - then add more bullets! 
   yield WaitForSeconds(reloadTime); 
    
   // We have a clip left reload 
   if (clipsLeft > 0 ) { 

      clipsLeft--; 
      bulletsLeft = bulletsPerClip; 
   } 
   SendMessageUpwards("setEnableWeaponSwitch", true); 
}