- Home /
WaitForSeconds is breaking out of my IEnumerator
I don't understand how this is possible but hear me out - when this IEnumerator is called:
[EDIT] method removed for security reasons.
All I get printed out in the console is "go1". I waited for over 2 minutes to see if anything else would be printed out.
Now if I comment out line 3 (yield return new WaitForSeconds(1.25f);) so that the yield doesn't get executed all my code is executed all the way down to the "whattt" being printed out in my console.
Why/how is it possible that the yield is breaking my coroutine. I've used tons of yields why is this one specifically ruining my code?
Just did a test, I replaced line 3 with yield return null; and my code worked. Once again something about the WaitForSeconds part is killing my code
Are you sure you don't have any additional code that could affect the coroutine? For example something that destroys the gameobject or the script which runs the coroutine? Without more information we can't say much about your problem. Generator functions are a language feature of C# so there's no issure there. The Unity coroutine scheduler also has a quite well defined behaviour. So it has to be something you do to the coroutine / gameobject / script.
Answer by trs9556 · Sep 15, 2013 at 11:33 PM
Like always there is a valid explanation.
What I did not know is WaitForSeconds is influenced by Time.timescale. I was setting my timescale to 0 therefore my waitforseconds was being held up.
Another item to keep note of, is that if the monobehaviour the coroutine was started becomes disabled, the coroutine will also stop. Although it doesn't apply here it's possible you may come across it in the future.
You can also make your timescale independent wait using Time.realtimeSinceStartup,
float startTime = Time.realtimeSinceStartup;
float waitTime = 3; //wait 3 seconds
while(Time.realtimeSinceStartup < startTime + waitTime) yield return null;
Your answer
Follow this Question
Related Questions
Coroutoutine doesnt work properly. 3 Answers
Please help with While Loops 1 Answer
Update() can not be a coroutine. 2 Answers
Custom Yield Instruction for FixedUpdate frames 3 Answers
[ Coroutine: Move Next ] CPU usage 0 Answers