- Home /
Coroutine is not called between frames
I start a coroutine in Start() like this: StartCoroutine("CoroutineName")
and inside it I use yield return null
to wait for another frame. But when I log calls to Update() and the coroutine, this is what I get:
Coroutine is not called between first two Update() calls. Why?
[EDIT]
Example code:
private void Start() {
Debug.Log("Start()");
StartCoroutine("ExampleCoroutine");
}
private IEnumerator ExampleCoroutine() {
Debug.Log("Called for the first time");
while (true) {
yield return null;
}
}
private void Update() {
Debug.Log("Update()");
}
Extended log:
Can you print something in the Start to see when it gets called. I would assume Coroutine yield instruction is a debug, you should also add one after the yield. With all that, it gets easier to see the process.
Where are the "yield return null" lines in your log co$$anonymous$$g from?
Answer by fafase · Mar 09, 2015 at 01:56 PM
Considering your new addition, there is nothing wrong.
First you get the Start printing which also starts the coroutine. The coroutine runs until the yield and returns to the Start that finishes. We get:
Start
StartCoroutine
Coroutine for first time
yield return null
(End of Start after the coroutine call)
Update
The first update is called as should. The frame is now done. Next frame
Coroutines are called after Update so the next frame update is called and finishes. The compiler finds a pending coroutine and runs it until it hits a yield or the end:
Update
yield return null
now all together:
Start
StartCoroutine
Coroutine for first time
yield return null
(End of Start after the coroutine call)
Update
Update
yield return null
Coroutines are called after Update so the next frame update is called and finishes. The compiler finds a pending coroutine and runs it until it hits a yield or the end:
Update
yield return null
now all together:
Start
StartCoroutine
Coroutine for first time
yield return null
(End of Start after the coroutine call)
Update
Update
yield return null
Update
yield return null
Coroutines are called after Update so the next frame update is called and finishes. The compiler finds a pending coroutine and runs it until it hits a yield or the end:
Update
yield return null
now all together:
Start
StartCoroutine
Coroutine for first time
yield return null
(End of Start after the coroutine call)
Update
Update
yield return null
Update
yield return null
Update
yield return null
... And so on
But what about coroutines being called after that first call to Update()? Do coroutines not get called after the Update() of the frame in which they were started?
Updated the answer and read it again as I explained why there is no call to coroutine after the first update.
@fafase Thanks for explanation. I solved my problem by starting coroutine after first Update().
private int frame;
private void Update() {
frame++;
if (frame == 2) StartCoroutine("ExampleCoroutine");
}
@fafase It's still not made clear why the coroutine doesn't get called after the first update. Specifically, you say...
"The first update is called as should. The frame is now done. Next frame"
This is problematic because, as you, show, generally speaking after an update the frame is not done, but ins$$anonymous$$d the engine (not the compiler I$$anonymous$$O) looks for pending coroutines and runs them.
I believe the answer is that because the coroutine in question has been run in that frame (when it was started), its "pending flag" has been cleared (and won't be reset till the start of the next frame). To me this seems like a crucial point to complete your otherwise excellent answer.
Ooooh ok I see now. Well yes, coroutine should not run twice so I guess it stores it for next frame.
Answer by alok-kr-029 · Mar 09, 2015 at 12:24 PM
Try
IEnumerator Example() { yield return new WaitForEndOfFrame(); }
Tried it, exactly the same result, no coroutine call between two first Update() calls.
its clear from the png that For first time the Update method is called twice in one frame I think so its due to its your function name as Update method once called automatically due to its behaviour and other due to your coroutine ,hence its called twice . I would suggest you to try changing the name of your function that you are calling
How are you calling it can you show the code
Your answer
Follow this Question
Related Questions
How to slow down large code? 1 Answer
void update working under conditions 1 Answer
Coroutine in Extension Method 1 Answer
WebGL prevent Unresponsive script warning 1 Answer
Load Level when dead for 1 second 2 Answers