How to pause a game in Unity

No matter what type of game you are making, having an option to pause the game is mandatory for good user experience. You can pause the game with or without a pause menu depending on your game type. In Unity, there are a few options when it comes to pausing a game. In this post we will see all the possible options to pause a game in Unity and also cover the advantages and drawbacks of each technique. So, let’s get started.

In Unity game development, the most common way to pause a game is using the timescale function. The other option is to use a Singleton to enable or disable the scene functions. The best option is to use both together but before that lets understand each one separately.

Using Time.timescale to Stop the time in Unity

This is the most basic and easy way to pause a game in Unity. All you have to do is to set Time.timescale to zero and the time inside Unity is stopped, this pauses most of the game. You can set it back to one and the game will resume. You can use this for most of your Unity project but it has some limitations.

Time.timescale affects all physics systems in the game and only the movement function related to time. What it means is if you have a character movement multiplied with time.deltatime, then the character will freeze and if you move your character by changing its transform inside an update function then the character will not freeze with timescale set to zero.

Time.timescale doesn’t freeze the following

  • Any action inside update not involving time.
  • Animation calls.
  • Update function.
  • UI interaction.
  • Other functions like collision detection.
  • Character movement that does not use Unity time function.

Using Singleton to pause a game

This method is very simple to explain but hard to implement. If you want better control on what is paused and what is not then, this is the best way to pause a game. All you need to do is to set the variable to true when you want to pause the game and check its status when implementing other movements.

What is a Singleton?

Singleton is a variable that is common for all scripts in your game. You can access it from any script in your game. If a Singleton value is changed in one script, the value gets changed in all scripts. Let’s understand with an example.

First let me define the health of my player as a singleton in a script called “player_health”.

public static int health=100;

Now I have a zone in which the health of the player starts decreasing. I have added this script in another gameobject called zone. To reduce the health of the player from the zone script I can use the following code.

if (player in zone)
{
player_health.health-=10;
}

This is the advantage of a singleton. You need not process everything inside a single script. Now let’s get back to how to pause a game in Unity.

Pausing a game with Singleton

It’s much similar to how we reduced the player health. You need to define a static Boolean variable which will be used to pause the game. I will declare the variable game_paused as static in the script pause_example and set it to false in the beginning.

public static bool game_paused=false;

In all the scripts where you are executing character movement or calculations, which you want to be stopped when the game is paused, you can include this variable in an if statement.

if(!pause_example.game_paused)
{
movecharacter();
}

Then you can simply set this variable to true to pause the game. This will ensure that the function is not called when the game is in the pause state.

Best way to pause a game in Unity

The best way to pause a game is to use both timescale function and a static variable wherever the timescale is not applicable. Earlier, we saw all the functions that are not paused when time scale is set to zero. Here are the functions that will get paused

Things that get paused

  1. FixedUpdate function is paused.
  2. All time related things are paused.
  3. Animations related to transform are paused.
  4. Get Input is paused.

So, to efficiently pause a game use the following method.

  1. Initialize a singleton and use it in Update function of scripts which you think should not run when the game is paused and set it to true when you need to pause the game.
  2. Set Time.timescale to zero.

Revert the above steps to resume the game.

The overall pause system of the game will look like the image below.

On Pause button press, set timescale to zero, set singleton value and show the pause menu to the user.

How to make a Pause menu in Unity

Now let’s create a basic pause menu with a resume button inside. When the user presses the pause button, we will set timescale to zero, set our Boolean singleton to true and make the pause menu to appear. We will reset all these when the resume button is pressed.

  • Click on the + sign in the Hierarchy window and go to UI>Panel.
  • Add a UI>button as child of the panel.
  • Add a UI text element as child of the panel to display the message “Game Paused”.
  • Set the anchors, so that your UI will scale with screen size.
Pause menu panel with a text and resume button

We need a button to pause the game.

  • Set the panel as inactive in the hierarchy.
  • Add a UI button to the scene and name it Pause. This button should not be a child of the panel we added earlier.
Pause button added to the scene and Pause menu panel is set to disabled.

Next, we need a script to do the pause operations.

  • Let’s create a new empty gameobject and call it GameManager.
  • Add a script called “Pause_Script”.
  • Here is the code for the script.
using UnityEngine;

public class Pause_Script : MonoBehaviour
{
    public GameObject Pause_menu;
    public void PauseGame()//called when the pause button is pressed
    {
        Time.timeScale=0;
        /amemanager.paused=true; //Set singleton to true
        Pause_menu.SetActive(true); //display Pause menu
    }
    public void ResumeGame() //called when the pause button is pressed
    {
        Time.timeScale=1;
        gamemanager.paused=false; //Set singleton to false
        Pause_menu.SetActive(false); //hide Pause menu
    }

}
  • Assign the panel to the Pause_menu variable

Now let’s link the public functions to our buttons.

  • Select the Pause button and go to the inspector window.
  • Click on the + sign in the On Click() property.
  • Drag and dop the game object with the above script the object reference.
  • Select the PauseGame function from the drop down.
  • Do the same for the Resume button. Remember to select the ResumeGame function in this case.
Assigning Pause function to the UI button

Now play the game and click on the pause button. You game should pause and the pause menu will be displayed. Click on the resume button to make the pause menu disappear.

That it for the tutorial, you can watch the video below which demonstrates how to use a pause menu with singleton and time scale function to pause a game in Unity.

Now it’s your time to try it out. If you have any questions, leave them in the comment box below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading