How to pause the game but not the menu

I’m making a game with a pause button. When I pause the game (Time.timescale=0) a GUI menu appears. The problem is that my menu buttons use animations, and animations don’t work if the timescale is stopped.

Any idea to solve this?

Don’t use Time.timeScale to pause, at least not directly.

Many people write a wrapper script, such as:

public static class GameTime {
    public static bool isPaused = false;
    public static float deltaTime { get { return isPaused ? 0 : Time.deltaTime; } }
}

This is a very simplified version of a wrapper just to demonstrate the concept.

In your game scripts, use GameTime.deltaTime instead of Time.deltaTime. In your Update methods, return immediately is GameTime.isPaused is true. For example:

void Update() {
    if (GameTime.isPaused) return;
    controller.Move(moveDirection * GameTime.deltaTime);
}
5 Likes

You can also set your Animator Controller > Update Mode to “Unscaled” which will allow your animations to play even if the game is paused / Time scale set to 0.

7 Likes

That was it! Now it works properly. Thanks a lot for your help, justoon

Thank you very much, TonyLi. Anyway, the answer of justoon is more useful for my purposes.

By the way, I’d like to ask you about the time wrapper class you suggest, if you don’t mind: What if your game uses physics? You can stop your own kinematic movements, but the physics engine wouldn’t stop, would it?

Agreed. That’s the better way to do it for GUIs.

Nope. You’d have to handle that yourself, or use something like Chronos. It’s nice that Unity added unscaled animation to make this a cleaner process.

Thanks for your answer, TonyLi

This seems like a good solution, I’ll give it a try!

don’t this is one of the worst solution, at least try to disable the monobehaviour instead of add a check in the update loop

1 Like