- Home /
why does Coroutine 'yield return 0' sometimes take a lot of frames before it continues
Hi I am having an issue where game object hangs around for a few seconds doing nothing at start up while the rest of the scene continues normally. The amount of frames it waits varies and is sometimes insignificant, but on occasions the delay can be very noticeable.
I eventually tracked it down to a 'yield return 0' and recreated it using this code
public class TestScript : MonoBehaviour {
int m_count = 0;
IEnumerator MyTest()
{
while (true)
{
print("-Before Yield " + Time.time);
yield return 0;
print("-After Yield " + Time.time);
}
}
public void Awake ()
{
print("-Starting Coroutine " + Time.time);
StartCoroutine(MyTest());
print("-Done Starting Coroutine " + Time.time);
}
// Use this for initialization
void Start () {
print("-Start Called " + Time.time);
}
// Update is called once per frame
void Update () {
print("-" + m_count + " Update Called " + Time.time);
++m_count;
}
}
If I create a new project, add an empty object and stick this in it it produces the following output, and in this case took 117 frames and 1.8 seconds before it continues
[10680] -Starting Coroutine 0
[10680] -Before Yield 0
[10680] -Done Starting Coroutine 0
[10680] -Start Called 0
[10680] -0 Update Called 0
[10680] -1 Update Called 0.02
[10680] -2 Update Called 0.2507744
[10680] -3 Update Called 0.2640077
[10680] -4 Update Called 0.2772415
[10680] -5 Update Called 0.2904751
[10680] -6 Update Called 0.3037083
.....
[10680] -112 Update Called 1.725368
[10680] -113 Update Called 1.751448
[10680] -114 Update Called 1.764683
[10680] -115 Update Called 1.777916
[10680] -116 Update Called 1.791149
[10680] -117 Update Called 1.804383
[10680] -After Yield 1.804383
[10680] -Before Yield 1.804383
[10680] -118 Update Called 1.817616
[10680] -After Yield 1.817616
[10680] -Before Yield 1.817616
[10680] -119 Update Called 1.830849
[10680] -After Yield 1.830849
[10680] -Before Yield 1.830849
[10680] -120 Update Called 1.844083
[10680] -After Yield 1.844083
[10680] -Before Yield 1.844083
[10680] -121 Update Called 1.857316
[10680] -After Yield 1.857316
and ingame it can be even worse and more noticeable. Especially if the framerate is a bit lower.
Is this a bug, is it something I'm (or more correctly the code that I found doing this) doing, is there a way to work around this. It has on occasion seemed like I've had to wait 20 or so seconds, but I haven't been able to re-create that so could just have been my imagination.
thanks Steve
further to that, just had this... more then 300 frames waiting and it 21 seconds !!!!
[12396] -Starting Coroutine 0
[12396] -Before Yield 0
[12396] -Done Starting Coroutine 0
[12396] -Start Called 0
[12396] -0 Update Called 0
[12396] -1 Update Called 0.02
[12396] -2 Update Called 0.3533334
[12396] -3 Update Called 0.5089295
[12396] -4 Update Called 0.6400028
[12396] -5 Update Called 0.7137224
[12396] -6 Update Called 0.781222
[12396] -7 Update Called 0.8522394
[12396] -8 Update Called 0.9256777
[12396] -9 Update Called 0.9945803
[12396] -10 Update Called 1.065505
[12396] -11 Update Called 1.139607
[12396] -12 Update Called 1.213301
.....
[12396] -299 Update Called 21.46553
[12396] -300 Update Called 21.52511
[12396] -301 Update Called 21.59686
[12396] -302 Update Called 21.69758
[12396] -After Yield 21.69758
[12396] -Before Yield 21.69758
[12396] -303 Update Called 21.7837
[12396] -After Yield 21.7837
[12396] -Before Yield 21.7837
[12396] -304 Update Called 21.88951
[12396] -After Yield 21.88951
[12396] -Before Yield 21.88951
[12396] -305 Update Called 21.98803
[12396] -After Yield 21.98803
[12396] -Before Yield 21.98803
[12396] -306 Update Called 22.09998
[12396] -After Yield 22.09998
[12396] -Before Yield 22.09998
I see this in Unity 4.2, albeit in a slightly more complicated situation (Awake creates an object then tells the created object to start a coroutine).
When I added trace I actually used Time.realTimeSinceStartup and that did something rather interesting which is that the early time values were higher than the later time values. I have a theory that this time difference is the time you will wait till the coroutine gets past the first yield, e.g. if Before Yield was 11 and UpdateCalled was 1 you will wait 10 seconds.
Was this ever logged as a bug? If not I'll report it.
Ok that sorts it.. I'll pass it on, and not sure why I didn't check that myself. any idea why putting it in awake ins$$anonymous$$d of start-up should cause this problem thanks Steve
No it doesn't - it appears that the coroutine system is not ready before Start() - OnEnable() exhibits similar behaviour. I'd report it as a bug.
Answer by JChilton · Apr 19, 2013 at 08:18 PM
Does the delay persist when you move the StartCoroutine() from Awake() to Start() ?
Your answer
Follow this Question
Related Questions
Inserting a delay 2 Answers
restart level when dead with delay 3 Answers
Coroutine not running after yield return new WaitForSeconds 3 Answers
WaitForSeconds Question 2 Answers
How to change a Texture with a Delay 1 Answer