- Home /
Using a unique time for yield WaitForSeconds
Hey guys,
I would like to use Time.timeSinceLevelLoad as the time used in yield WaitForSeconds. How would I be able to do this?
EDIT: What I am trying to do is when Time.timeScale = 0; yield WaitForSeconds still works as it is supposed to use timeSinceLevelLoad so that it can still yield while not using Time.time as it would be using Time.timeScale which is 0.
Answer by FakeBerenger · Nov 07, 2012 at 11:14 PM
Simply :
JS
yield WaitForSeconds( Time.timeSinceLevelLoad );
C#
yield return new WaitForSeconds( Time.timeSinceLevelLoad );
However, beware, that might be a long time if the player starts the game, go grab a coffee or whatever.
Answer by aldonaletto · Nov 07, 2012 at 11:13 PM
If I understood correctly what you want, just pass Time.timeSinceLevelLoad to WaitForSeconds:
yield WaitForSeconds(Time.timeSinceLevelLoad);
But this seems weird: you will get a delay that expires at twice the level duration at the moment this instruction is executed - is this what you want?
Answer by whydoidoit · Nov 08, 2012 at 02:36 PM
Take a look at my tutorial on Unity Gems on coroutines - the video for that includes building a version of WaitForSeconds that uses realTimeSinceStartup and hence works when the timeScale is 0. http://unitygems.com
The video is quite long :S You'd be looking to work somewhere around the 15 minute mark I seem to remember.