How to Freeze and Unfreeze my game?
Asked Answered
S

3

0

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

Shearer answered 8/11, 2023 at 18:33 Comment(0)
Y
0

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.

Yamen answered 8/11, 2023 at 20:42 Comment(0)
L
0

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;.

Lightner answered 8/11, 2023 at 21:38 Comment(0)
E
0

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.

Euglena answered 25/4 at 3:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.