Unity - Invoke or StartCoroutine not running when timeScale = 0
MonoBehaviour.Invoke not running in Unity

Recently I have faced a weird error in Unity: my Invoke of functions are not called. This confuses me because there are no errors or exceptions are thrown. Turn out, in specific scenarios in my game, I set Time.timeScale = 0, then everything is freezing.

When timeScale is set to zero the game is basically paused if all your functions are frame rate independent.

My first thought was switching to Coroutine:

IEnumerator MyCouroutine(float waitTime)
{
 yield return new WaitForSeconds(waitTime);
 //DO STUFF
}

But this does not work also, the code will never finish yielding. WaitForSeconds is still depend on frame rate.

The real fix for this was relatively similar. You can change WaitForSeconds to WaitForSecondsRealtime then it will work as intended.

WaitForSecondsRealtime is frame rate independent.

So the correct code is below:

IEnumerator MyCouroutine(float waitTime)
{
 yield return new WaitForSecondsRealtime(waitTime);
 //DO STUFF
}