r/UnityHelp • • Sep 29 '26

PROGRAMMING Question about Pausing

I have two different things in my game that are supposed to pause the game/set the timescale to zero. At some point somehow one of the two stopped pausing the game but the other still works. They both say the exact same thing, simply setting time.timescale=0. For the record the one that doesn’t work is the when a button is pressed and the pause/stats screen comes up. Every other thing that is called in that function works expect the time scale. Can you not have two different functions that can pause the game or something? What could be the issue here?
Thanks!

1 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/Patient_River_229 Sep 30 '26

Although one thing I should note (I’ve been trying to figure out how to prevent this for like an hour now and not sure how to do it). There is a very niche scenario where the player could die while one of the paused scenarios is in effect, which then brings up the game over and menu scenes which just softlock the game in time scale 0 because necessary animations can’t play. I’m trying to figure out how to quickly unpause before the player dies in that scenario or to unpause on start at the game over screen. Though it seems I can’t just have a separate script to make timescale =1 on start since that conflicts with this whole system

1

u/Demi180 Sep 30 '26 edited Sep 30 '26

If this scenario where the player dies while paused is intentional, I'd definitely recommend against allowing that to begin with, but it's still a good safety to ensure the game is or isn't paused when game over happens, as necessary.

If you need something to happen or not happen while the game is paused, just look to this Property I put in there:

public static bool Paused { get; private set; }

You can use it from anywhere by referencing Pauser.Paused. As far as unpausing for the game over, again there are lots of ways. You could add a method to the Pauser that will force the game unpaused, but you do need to make sure not to abuse it. Something like this:

public static void ClearPause()
{
  pauseSources.Clear();
  SetPause(false);
}

Or you could have the various objects that can pause the game "listen" for the player death / game over "event" so they can each undo their own pause. The Observer Pattern is very handy for these sorts of things where you want something to be notified when something else happens. You can use C# delegates/events or UnityEvents to implement that.

Notice I'm putting language features and programming concepts in bold and Unity API in monospace to let you know you can look them up 🙂