How do i make a repeatable timer

So this script is basically the start of something im trying to expand, I want to make a window timer in which i can act after pressing a button.
This currently requires me to hold 4 and then left click to instantiate the object, but I want it so when i press 4 there’s 5 seconds to be able to left click, and for the life of me i havn’t been able to either write the correct way or place it properly

	var particle:GameObject;
	public var gravFlux:AudioClip;
	private var canFire:boolean = true;
	
	function Update () {
		if(Input.GetKeyDown("4")) EnableClick();
        	if(canFire && Input.GetButtonDown("Fire1"))
				FireandReload();	//Calls the below function
		
	}
	
	function EnableClick() 
    {
          canFire = true;
          yield WaitForSeconds(5);
          canFire = false;
    }
	
	function FireandReload (){
		if (Input.GetButtonDown ("Fire1")) {
	// Construct a ray from the current mouse coordinates
		var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;
			if (Physics.Raycast (ray, hit)) {
	// Create a particle if hit
				Instantiate (particle, hit.point, transform.rotation);
					canFire = false; //Script can now no longer player again
						yield WaitForSeconds (5);
							canFire = true; //After 3 second delay script is reset
			}
		}
	}

1 Answer

1

Try this:

    var canFire : boolean;

    function Update()
    {
         if(Input.GetKeyDown("4")) EnableClick();
         if(canFire && Input.GetButtonDown("Fire1"))
         {
                 //Do something
         }
    }

    function EnableClick() 
    {
          canFire = true;
          yield WaitForSeconds(5);
          canFire = false;
    }