- Home /
Does coroutines are affected by Time.timeScale?
When I create an IEnumerator like this one:
IEnumerator Example () {
yield return new WaitForSeconds(20);
//do something
}
Let's say that in another script I set Time.timeScale = 0 for some time. When I do this, the coroutine continue running to complete the 20 seconds or it pauses while timeScale is equals to 0?
Answer by aldonaletto · Jun 29, 2012 at 02:08 AM
Setting timeScale to 0 doesn't stop coroutines, but stops WaitForSeconds. Coroutines continue executing each frame, but WaitForSeconds apparently uses the current Time.deltaTime, which becomes zero when timeScale = 0.
The simple test below shows this:
function TestCoroutine(){
for (var n=0; n < 5; n++){ // count 5 frames...
print("frame "+n);
yield;
}
print("start delay 5 s");
yield WaitForSeconds(5);
print("delay ended");
}
var pause = false;
function Update () {
if (Input.GetKeyDown("escape")){
pause = !pause;
if (pause){
Time.timeScale = 0;
TestCoroutine();
} else {
Time.timeScale = 1;
}
}
}
When Escape is pressed, timeScale is set to 0 and TestCoroutine is started - the 5 frames are counted and WaitForSeconds(5) is started, but the 5 seconds wait time lasts forever until you press Escape again to return timeScale to 1.
Did you try something akin to this:
public static IEnumerator WaitForRealSeconds( float delay )
{
float start = Time.realtimeSinceStartup;
while( Time.realtimeSinceStartup < start + delay && cancelShowngTooltip == false )
{
yield return null;
}
}
private IEnumerator ShowTooltipAfterDelay( float delay )
{
yield return StartCoroutine( WaitForRealSeconds( delay ) );
if( cancelShowngTooltip == false )
Debug.Log( "Show Tooltip now." );
}
Answer by $$anonymous$$ · Jun 11, 2018 at 03:17 PM
Yes, it affects and it's effect is normal when you keep that game objects which contains coroutine ON, While it is on every thing works fine. If you TURN OFF the game object then every thing going to be mess.
Your answer
Follow this Question
Related Questions
How can you tell if a gameobject is looking directly at another gameobject? 1 Answer
WaitForSeconds Not Working 4 Answers
Problem with coroutine 2 Answers
How Return Or Restart A Coroutine When A Variable Increase? 0 Answers
Ienumerator wait for event 0 Answers