GetButton delay not working properly?

The idea is for the function to happen after button is held for length of time, but only once, then waits for length of time again, then function completes again, but if button is not being pressed anymore, delay is false.

The problem is it waits, but then carries out the function many times. I think it is due to using GetButton instead of GetButtonDown, but I need to use GetButton in order for it to loop. Unless there is a better way?

var gbDelay : boolean = false;

function Update(){
    if(Input.GetButton("button")){
    		Delay();
    		Debug.Log("started delay");
    			if(gbDelay==true){
    				
    				//do rest of function
    				gbDelay = false;
    				}	
    				}
    else {
    gbDelay = false;
    }
}

function Delay (){
yield WaitForSeconds (10);
Debug.Log("Time Waited");
gbDelay = true;
}

Your approach of using “yield” above has some problems since Delay() will return almost immediately. It is just the code below the WaitForSeconds() that will wait 10 seconds before executing. My approach would be to track the down and up events and not worry about the getting the events in the middle, and I’d use my own timer. If the button comes up, the timer stops and is reset to 0 on the next down button. Here is a script. It uses GetKeyDown() and tracks the space bar, but the logic is the same.

public var delayTime = 3.0;
private var isDown : boolean = false;
private var timer : float = 0.0;

function Update () {

	if (Input.GetKeyDown(KeyCode.Space)) {
		timer = 0;
		isDown = true;
	}
	
	if (isDown) {
		timer += Time.deltaTime;
		if (timer >= delayTime) {
			isDown = false;
			Debug.Log("Button Fired"); 
		}
	}
	
	if (Input.GetKeyUp(KeyCode.Space))
		isDown = false;

}

This ended up being what I needed. I should have checked out the docs before hand. Shame on me. I didnt have the same purpose as fire rate, but doesn’t matter. You can make a nice subtle delay in a repeating function with this.