- Home /
WaitForSeconds get stuck after update of Unity Pro
After I updated to Unity Pro 3.5.6f4 my (previously working) script stopped working:
IEnumerator Start () {
// More code here...
Screen.SetResolution(1920, 1080, true, 0);
Debug.Log("Begin yield");
Debug.Log(Time.timeScale);
// Wait for new screen resolution to take effect
yield return new WaitForSeconds(1.0f);
Debug.Log("Done yielding");
// More code here...
}
The output from the above log is:
Begin yield
1
So I can not get past the yield statement. I have tried to remove and reapply the script. I have also tried to do "Reimport all" on my project. All without success. Any ideas??
Hmmm, are you sure you aren't disabling or destroying the object? I've not seen any problems with the latest release so I would imagine it is something in your code.
Well the script is connected to the camera and I am pretty sure that is not deleted nor disabled.
This guy had a similar problem but his time-scale was set to zero (but my time-scale is one): http://answers.unity3d.com/questions/60725/waitforseconds-problem-with-unity-pro.html
So if you look at this script in the inspector then it's game object is checked active and so is it's particular script?
Yes, both the game object (camera) and the script are active.
How strange. Hmmm, well with a valid timeScale and an active object - I've not seen that behaviour and I pretty much use this technique in Start all the time. If you can easily replicate then you should report it as a bug.
$$anonymous$$ight be worth adding a Debug.Log in an OnDisable method just to prove the point.
Answer by BjornVTI · Oct 30, 2012 at 04:29 PM
The answer in this case was that another script set the Time.timeScale to zero. This was not visible outside the yield statement. But was clearly visible by running:
float delay = 1.0f;
print ("Start Time:\t" + Time.time);
print ("Time scale:\t" + Time.timeScale);
float done = Time.time + delay;
while(Time.time < done) {
print ("Time:\t"+ Time.time + "\tRealtime:\t" + Time.realtimeSinceStartup + "\tTimeScale:\t" + Time.timeScale);
yield return 0;
}
Which gave me the following output:
Start Time: 0
Time scale: 1
Time: 0 Realtime: 0.15797 TimeScale: 1
Time: 0 Realtime: 0.8051 TimeScale: 0
Time: 0 Realtime: 1.154805 TimeScale: 0
Time: 0 Realtime: 1.188964 TimeScale: 0
Time: 0 Realtime: 1.19767 TimeScale: 0
Which made the real problem visible (ie. the time scale gets set to zero)
Answer by BjornVTI · Oct 30, 2012 at 01:50 PM
After an idea from: http://answers.unity3d.com/questions/14785/waitforseconds-vs-yield-every-frame.html
I tried to replace the waitforseconds instruction with the following instead
float delay = 1.0f;
print ("Start Time:\t" + Time.time);
print ("Delay:\t" + delay);
print ("Time scale:\t" + Time.timeScale);
float done = Time.time + delay;
while(Time.time < done) {
print ("Time:\t"+ Time.time + "\tRealtime:\t" + Time.realtimeSinceStartup);
yield return 0;
}
But this gives me the following output:
Start Time: 0
Delay: 1
Time scale: 1
Time: 0 Realtime: 0.1746936
Time: 0 Realtime: 0.8073034
Time: 0 Realtime: 0.9108546
Time: 0 Realtime: 1.17599
Time: 0 Realtime: 1.183453
Time: 0 Realtime: 1.199383
Time: 0 Realtime: 1.207282
Time: 0 Realtime: 1.218535
So for some reason the time is standing still.
Add a print of Time.timeScale inside the while loop too - just to make sure something isn't setting it to 0.
As you suspected, Time.timeScale is zero inside the loop. Good catch, now I have something to work with!
Well, I found it. Someone on our $$anonymous$$m had made some changes inside another script, and that change set the timeScale to 0.
Thank you whydoidoit for helping me solve this!