TimeScale = 0 crashes Unity

I recently implemented a Pause menu into my game, just switching the timescale between 0 and 1.

This code causes Unity to crash:

Code:

function Update(){
 
if(myTransform.position == Waypoints[currentWaypoint] && currentWaypoint < 3){
            currentWaypoint = currentWaypoint + 1;
            Go();
}
}
 
function Go(){
 
for(t = 0; t<1;){
    t += Time.deltaTime * speedMod;
        myTransform.position = Vector3.Lerp(myTransform.position,Waypoints[currentWaypoint],t);
        if(myTransform.position != Waypoints[currentWaypoint]){
            yield;                         
        }
 
}
}

I understand why it’s freezing, it gets stuck in the for loop because deltaTime = 0.

So I’ve tried changing my pause menu to set timeScale to 0.0001. This helps, but it still gets stuck in the loop and I’m unable to unpause till the loop finishes.

I’ve also tried to add a break in the loop if the timeScale == 0.0001. This fixes the pausing and unpausing but causes my lerping to stop working if paused.

Can anyone give me a hand with this?

How do I handle deltaTime loops when changing the timeScale? What are my options?

Of course it does. Why do you have this if around the yield?

if(myTransform.position != Waypoints[currentWaypoint]){
    yield;                         
}

Time.deltaTime will be 0.0 when timescale is 0.0 so this condition never gets true so your for loop loops endless without any change. Just remove the if statement and execute the yield unconditional.

edit
Hmm, actually the condition should be true, but anyway the most important thing in a coroutine is to execute yield. A lot coroutine setups uses a while(true) loop which will never end, but it’s ok as long as you call yield, every iteration.