- Home /
yield return new waitforseconds(5f) doesn't finish
I have written the following piece of code and the problem is that the WaitForSeconds Method doesn't exit / finishes and I don't know why.
IEnumerator increaseDelay(){
ground.disable();
float oldDelay = spawn.getDelay ();
spawn.setDelay (spawnRate);
yield return new WaitForSeconds (5f);
spawn.setDelay (oldDelay);
ground.enable ();
}
Answer by Bunny83 · Sep 28, 2017 at 11:25 PM
The only reasons why a coroutine won't finish executing is:
You destroyed the gameobject or the MonoBehaviour which the coroutine runs on. The hosting MonoBehaviour is the one where you used StartCoroutine, not necessarily where the coroutine is located.
You deactivated the gameobject or disabled the hosting MonoBehaviour.
You called StopAllCoroutines or StopCoroutine manually which stops the execution.
Keep in mind that coroutines can only be stopped / aborted at "yield points" as those are the points where the control is passed back (yielded) to Unity. At this time you most likely performed any of the above mentioned steps which will terminate the coroutine at the current point.
Coroutines are not "methods" but are translated into statemachine objects. Each call of "MoveNext" on the IEnumerator object will advance the statemachine to the next yield. Unity will do that internally. It inspects the "Current" value of the statemachine and based on that value it decides when to resume this coroutine.
When the MonoBehaviour is destroyed the coroutines stored internally will simply be removed along.
First of all huge thanks for your long explanation :) And and I didn't know that the Coroutine stops after I had destroyed the Object. So thanks again and I think I can now fix this on my own :)
Very helpful explanation. $$anonymous$$y problem was that I destroyed the gameobject hosting my coroutine.
OR you stopped time (Time.TimeScale = 0). In this case use WaitForSecondsRealtime.
Answer by tulaib-pirzada · Oct 17, 2018 at 03:22 AM
Following can be reasons due to which your coroutine gets stuck on WaitForSeconds
:
The time given in
WaitForSeconds
is too long. Although this is silly reason, but sometimes this happensThe object from which you are calling the coroutine is getting destroyed. You can check that as follow.
void OnDestroy() { Debug.Log("Destroyed"); }
From any place,
StopAllCoroutines()
is getting called which is stopping your coroutine.Lastly, check that anywhere
Time.timeScale
is getting set to 0. Because when its zero, it stopsTime.deltaTime
from increasing and as a resultWaitForSeconds
never reaches your specified value.You can check your current timescale inEdit -> Project settings -> Time
check Time Scale parameter in that. Or you check its value by logging before your coroutine is called.
Answer by FernandoGBR · Sep 28, 2017 at 04:18 PM
add
yield return null;
at the end of the coroutine.
If I am not wrong coroutines wont execute after the last yield return statement. Please correct me if I am.
Are you changing the TimeScale in spawn.setDelay()
?
Try to write something in console after calling your functions. I am nearly sure that if you are nomt changing the time scale there should be no problem with your new wait for seconds
call
No, this is wrong. Coroutines (or generator methods in general) do execute the code after the last yield. This is the code that is executed when the coroutine actually finishes. Yielding once again at the end is pointless.
Answer by mwmwmw · Dec 16, 2021 at 08:48 PM
I have run into this issue before, and it happens when I call my coroutine directly like a method.
It's an easy mistake to overlook.
The proper way to call a coroutine is: StartCoroutine(myFunction());
Answer by Durium · Mar 20 at 05:35 PM
Had to use "WaitForSecondsRealtime()" since i am altering the Time.timeScale = 0... If anyone has the same iossue trey this