How do I decrease a number by 1 after a certiam amount of time has passed.

Hey,

I am trying to create a powerup system in my game right now and the way that I have set it up is if the player passes through an object a number will increase by 1. And after the player passes through this object 4 times the powerup will get enabled. What I am trying to add to this is when the player passes through the object once the number gets set to one after a certain amount of time passes without the player going through the object again the number goes down by 1. If the player goes through the object before the timer reaches 0 then the number should go up by 1 again. This is what I have tried.

while(comboNumber >= 1)
    {
        StartCoroutine(ComboWait());
        comboNumber -= 1;
    }

IEnumerator ComboWait()
{
    yield return new WaitForSeconds(1f);
}

All that is happing from this is the comboNumber is never going up. It always stays at zero. I have tried adding a bigger delay but the comboNumber never seems to go up. The while loop is in the update function. If anybody has any suggestions that would be great!

Thanks

  1. Do NOT use while() Coroutine loop in Update, you’re implicating that you want the game to start a coroutine every single update. Imagine the game run 60 updates per second, and you want the engine to wait 1 second every frame to finish the coroutine.
  2. You aren’t using Coroutine correctly.

firstly, to have Update() runs nicely with Coroutine, you’ll have to add condition check if the Coroutine is running, otherwise you’ll run 60 Coroutine simultaneously every second, then reset the Coroutine if you pass through the object again, which, to do that, you’ll have to set a reference to the Coroutine object.

a simpler way to do this is to ditch Coroutine completely.

float comboDecayCountdown; //when this value reach 0, the decay is completed, comboNumber will reduce
    float comboDecayTime = 1f;

    void OnPassThroughObject() //when player pass through the mentioned object
    {
        ResetDecay();
        comboNumber++;
    }

    void ResetDecay()
    {
        comboDecayCountdown = comboDecayTime;
    }

    void Update()
    {
        if (comboNumber >= 1)
        {
            if (comboDecayCountdown > 0)
            {
                comboDecayCountdown -= Time.deltaTime;
            }
            else
            {
                comboNumber--;
                ResetDecay();
            }
        }
    }

secondly, if you want to get a wait time from StartCoroutine, you do it like this (if you use the Update() code above, you don’t need these codes)

    IEnumerator ComboDecay()
    {
        yield return StartCoroutine(ComboWait());
        comboNumber--;
    }

    IEnumerator ComboWait()
    {
        yield return new WaitForSeconds(1f);
    }

Thanks so much! What was suggested did work!!

Thanks again

What you want to do is this:

 StartCoroutine(ComboWait());
   
 IEnumerator ComboWait()
 {
     while(comboNumber >= 1)
     {
          yield return new WaitForSeconds(1f);
          comboNumber -= 1;
     }
 }