Running a Coroutine for a specified time.

IEnumerator DecreaseTime ()
{
while (timeLeft >= 0.0f) {
while (isPaused) {
yield return new WaitForEndOfFrame ();
}
timeLeft = timeLeft - 0.01f;
Debug.Log (timeLeft);
yield return new WaitForSeconds (0.01f);
}

}

The timeLeft variables value is 10 at the startOfCoroutine but than i want to decrease the timeLeft with 100th part of second because this time is used for progress bar and that way i can make it look smooth.But this coroutine doesn’t run for 10 seconds it runs for something like 22.6 seconds.

Thank you.

A coroutine can only run at max with the current frame rate. WaitForSeconds will wait the at least the specified time but the coroutine can only be resumed once each frame. “0.01” would require at least a framerate of 100 fps.

You should do something like this instead:

     timeLeft -= Time.deltaTime;
     yield return null;

The problem is inaccuracy in return new WaitForSeconds (0.01f); and in the line
timeLeft = timeLeft - 0.01f;

Try this here:

IEnumerator DecreaseTime ()
 {
     while (timeLeft >= 0.0f) 
   {
        if (!isPaused) 
        {
              timeLeft -= Time.deltaTime;
         }

       // yield return new WaitForEndOfFrame ();
      yield return null;  // props to Bunny83 for this
  
     }
 }