Hello, guys! I have a question. When I am pausing my game, I want it to freeze and when unpausing I want it to unfreeze. I know basically how to achieve that but I don’t know how to make that, when I have also IEnumerators in my script. So, What I did was I wanted with a specific key to pause and unpause game with a delay. So, when I am trying to pause my game with Time.timeScale it pauses but never unpauses the game because it stuck on freeze.
Check my code here:
BlazeBin - msbkrpgxcsmh
I assume that by “having IEnumerators” you mean Coroutines.
The easiest way is to set static variable Time.timeScale = 0;
, just make sure that your UI animations and stuff you don’t want frozen uses unscaled time.
The best way to freeze a game in Unity is to set Time.timeScale
to 0
Time.timeScale = 0;
, which effectively pauses the game.
To unfreeze, you would reset Time.timeScale
to 1
Time.timeScale = 1;
.
TL;DR: the code but I figured you want a coroutine keep going even if timescale is zero.
So, if you want the coroutine to keep going with timescale 0, you should use WaitForSecondsRealtime
instead of WaitForSeconds
inside them. Also be aware of existence of Time.unscaledTime
and Time.realtimeSinceStartup
you might want to use to do some stuff when the game is paused.