Why will this yield not work?

using UnityEngine;
using System.Collections;

public class CooldownTest : MonoBehaviour {

    public bool onCooldown = false;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {

        if (onCooldown == false  Input.GetKeyDown(KeyCode.H)) {
            onCooldown = true;
            Cooldown();
        }
        Debug.Log("Pressing H: " + Input.GetKey(KeyCode.H));
	}

    IEnumerator Cooldown() {
        yield return new WaitForSeconds(3);
        onCooldown = false;
    }
}

This code is supposed to be so that when you press H, it sets the bool as true and goes directly to the coroutine, waiting 3 seconds and then setting the bool to false. Why will it not work? Thanks

You need to call your coroutine with StartCoroutine(Cooldown()); :slight_smile:

Thanks :slight_smile: But now I have the problem, the coroutine starts when the game loads, and not when the player presses H. But I can’t put the StartCoroutine in the Update beecause Update updates it every frame…what do I do?

You need to make sure that it is only called once. Some lines of code that should guide you in the right direction: :wink:

using UnityEngine;
using System.Collections;

private bool isCoolingDown = false; 

public class CooldownTest : MonoBehaviour {
...
}
void FixedUpdate(){
...
    if(Input.GetKeyDown(...)  !isCoolingDown){
        StartCoroutine(...);
    }
...
}
IEnumerator Cooldown() {
isCoolingDown = true;
...
isCoolingDown = false;
}