I need help with an IEnumerator.

So i was trying to make a script that would make the play invisible for 3 seconds when the player hits the space bar and then he could use the ability again after 20 seconds, but when he presses space again the player just starts flickering between tags and Color.

Here’s my code:

public PlayerController player;

    public Image imageCooldown;
    public float cooldown = 20;
    public float duration = 3f;
    bool isCooldown;

    public SpriteRenderer sr;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            isCooldown = true;
        }

        if (isCooldown)
        {
            StartCoroutine(Invisible());

            imageCooldown.fillAmount += 1 / cooldown * Time.deltaTime;

            if (imageCooldown.fillAmount >= 1)
            {
                imageCooldown.fillAmount = 0;
                isCooldown = false;
            }
        }
    }

    IEnumerator Invisible()
    {
        player.transform.tag = "StealthPlayer";
        sr.color = new Color(1f, 1f, 1f, .80f);

        yield return new WaitForSeconds(duration);

        player.transform.tag = "Player";
        sr.color = new Color(1f, 1f, 1f, 1f);
    }

While “isCooldown” is true you start a new coroutine every frame.

You probably wanted something like that:

    if (!isCooldown && Input.GetKeyDown(KeyCode.Space))
    {
        isCooldown = true;
        StartCoroutine(Invisible());
    }
    if (isCooldown)
    {
        imageCooldown.fillAmount += 1 / cooldown * Time.deltaTime;
        // [ ... ]

Note that your cooldown starts immediately when the player activates the ability. Maybe it would make more sense to actually chain those times. If you do you could do everything in a single coroutine. Starting coroutines is quite expensive. Something like that would work as well:

void Start()
{
    StartCoroutine(Invisible());
}

IEnumerator Invisible()
{
    while (true)
    {
        // wait until the space key is pressed
        while (!Input.GetKeyDown(KeyCode.Space))
            yield return null;
        
        // activate ability
        player.transform.tag = "StealthPlayer";
        sr.color = new Color(1f, 1f, 1f, .80f);
        
        yield return new WaitForSeconds(duration);
        
        player.transform.tag = "Player";
        sr.color = new Color(1f, 1f, 1f, 1f);
        
        // cooldown
        float step = 1f / cooldown;
        for(float f = 0f; f < 1f; f += step * Time.deltaTime)
        {
            imageCooldown.fillAmount = f;
            yield return null;
        }
        imageCooldown.fillAmount = 1f;
    }
}

Now your cooldown time starts once the ability duration is over and not immediately. Instead of

yield return new WaitForSeconds(duration);

you could use the reverse of the cool down:

        float step = 1f / duration;
        for(float f = 0f; f < 1f; f += step * Time.deltaTime)
        {
            imageCooldown.fillAmount = 1f - f;
            yield return null;
        }

With this your cooldown bar would rapidly go backwards while the ability is active. With this setup you could easily implement an interruptable ability. So when pressing space you could actually toggle the ability on and off. A bit like the TF2 invisibility watch. Though considering all mechanics in TF2 it’s a lot more complex

Thank you! I will test it right now.