Pausing a Game in Unity

By: unevolvedchansey


In the realm of Unity as an engine at least, odds are one of the first things you read about when looking to pause your game, is a little thing called Time.timeScale.

For the non-programmers out there, I'll summarize;

Games are basically a series of conditional checks (Is the player doing anything? Are those enemies still alive? ) that check themselves over and over, until something tells it to stop (Did someone go back to the main menu? Did the application close?).

Time.timeScale is the rate at which these looping checks occur. If your timescale is high, then your game runs faster than it normally would. Similarly, if it's zero, nothing in your game is running. Which means it's perfect for Pausing!


Eh. You'd think that, until you realize that you can't just tell a game to "STOP EVERYTHING" and expect it to look good. Things like menu animations and cool little cutscene animations while the action is paused still depend on having the game still technically running, while you selectively pause individual bits. To this end, you end up creating code that, before it does absolutely anything else, checks whether the game's Pause Manager says the world should be paused or not, instead of little shortcuts like timescale. 

For Morty in particular, it was an... entertaining process to sort out how to handle events between programming styles; without throwing my hands into the air and just rewriting the lot of it. At the end of the whole process, I ended up having everything that used to run on an Invoke() call delayed by x seconds, work as a coroutine instead. Then any object that had to be paused, would find the Pause Manager, say "Hello my master! I will be your willing subordinate!", and pause whichever bits needed to be put in stasis when a player presses pause. 

Physics, Updates, Particle Systems and Object Movement scripts in particular had events for when the game Pauses() and UnPauses(), which let us move forward with letting menus animate; or selectively pausing, say, the bulls, while Morty does the cancan.

Stay tuned for more news and updates on Morty the Matador!