problem with coroutines

So I finished the Space Shooter tutorial and I’m trying to add a powerup that will allow the player to shoot three shots instead of one for ten seconds. My problem is that when I run my coroutine to switch my “TripleShot” Boolean to false after ten seconds, the Boolean stays at false for an additional ten seconds even if I pick up the powerup again. I feel like I’m missing something blatantly obvious but I spent about two hours trying to fix it without luck.

Here is the coroutine

    private IEnumerator TripleShotTimer()
    {
        yield return new WaitForSeconds(10);
        TripleShot = false;
        StopCoroutine(TripleShotTimer());
    }

Hey you!

I think your problem is the StopCoroutine part of it. If you pick up a second triple shot power up, you probably want to reset the little timer, right? Well, you aren’t actually stopping the timer of the first powerup when you pick up a second. That StopCoroutine isn’t doing it. Honestly, I’m not quite sure what it is doing, but that’s because of my inexperience probably. I could look it up, but oh well. To use the StopCoroutine, I think it would be best to give it a reference to the last powerup coroutine that you started. That way it actually has a specific thing to stop. Here’s some code that I think can accomplish this for you. I have it starting with the Space button, but that was just for testing.

public bool tripleShot = false;
private Coroutine enableTripleShotCoroutine = null;

    
void Update () {
    if (Input.GetKeyDown(KeyCode.Space)) {
        // Stop an existing timer for the triple shot powerup if it exists
        if (enableTripleShotCoroutine != null)
            StopCoroutine(enableTripleShotCoroutine);

        // Start and save new powerup timer coroutine
        enableTripleShotCoroutine = StartCoroutine(EnableTripleShot(10.0f));
    }
}

// Coroutine that handles the timer of the triple shot powerup
private IEnumerator EnableTripleShot(float duration) {

    // Timer begins
    tripleShot = true;

    float elapsedTime = 0;
    while (elapsedTime < duration) {

        // Do something with the time remaining if you want
        float timeRemaining = duration - elapsedTime;
        Debug.Log("Time remaining: " + timeRemaining);

        // Increase elapsedTime
        elapsedTime += Time.deltaTime;
        yield return null;
    }

    // Timer is complete!
    tripleShot = false;
}