How to Pause the Game in Unity Properly

Introduction

When creating a game in Unity, you may want to add a pause function. This can be achieved by setting the time scale to zero to pause the game and back to one (the default) to unpause it. The script for pausing the game looks like this:

void PauseGame() { 
  Time.timeScale = 0;
}
void ResumeGame() { 
  Time.timeScale = 1; 
}

This script can be called by a button or key press to pause and unpause the game. In this tutorial, we will discuss how to pause and unpause the game, what gets paused and what doesn’t, how to pause everything in the game except for certain objects, how to use a coroutine when the game is paused, and how to animate menus and objects when the game is paused.

What does and doesn’t get paused

When the game is paused, certain things will continue to function normally while others will be stopped. The Update function will continue to be called, but FixedUpdate doesn’t get called at all. Time will be stopped, which means that Delta time based movement will be stopped. For example, the following code will not work when the game is paused:

void Update() { gameObject.transform.Translate(Vector3.down * Time.deltaTime); }

However, if you are using a fixed multiplier for movement, it will not be paused. For example:

void Update() { // Don't do this! gameObject.transform.Translate(Vector3.down * 0.02f); }

How to pause everything in the game except for certain objects

If you want to pause everything in the game except for certain objects, you can use unscaled time. This will ensure that the time is not paused for these objects. You can do this by using the following code:

Time.timeScale = 0; Time.fixedDeltaTime = 0;
// Pause everything except for certain objects foreach (GameObject obj in certainObjects) { obj.GetComponent<Rigidbody>().isKinematic = true; } Time.timeScale = 1; Time.fixedDeltaTime = 0.02f;

How to use a coroutine when the game is paused

When the game is paused, most coroutines will be frozen. However, you can use Time.unscaledDeltaTime to allow the coroutine to run on unscaled time. Here’s an example:

IEnumerator MyCoroutine() { 
  while (true) { 
    yield return new WaitForSecondsRealtime(1f); 
    Debug.Log("This is printed every second, even when the game is paused"); 
  }
}

How to animate menus and objects when the game is paused

When the game is paused, menu animations based on time will be paused. To solve this problem, you can change the Update Mode of the Animator component to use Unscaled Time. Here’s how to do it:

  1. Select the Animator component of the object you want to animate.
  2. In the Inspector window, change the Update Mode to Unscaled Time.

How to prevent control input when the game is paused

When a game is paused, it’s usually desirable to prevent player input from being processed. If you’re using Unity’s new Input System, preventing input when the game is paused is easy.

If you use the old input system? Please don’t use the old Input system in 2023

First, make sure you’re using the new input system. If you’re still using the old input system, it’s recommended to switch to the new input system. To do so, navigate to Edit -> Project Settings -> Input System Package, and select “New Input System Package”.

Once you’re using the new input system, you can prevent input when the game is paused by changing the update mode of the input system to “Process Events in Dynamic Update”. To do so, open the Input System settings by navigating to Edit -> Project Settings -> Input System. In the settings window, scroll down to the “Update Mode” option, and select “Process Events in Dynamic Update”.

With this update mode, the input system will only process input events during the dynamic update phase, which occurs after the fixed update phase. Since the fixed update phase is not called when the game is paused, input will be effectively disabled when the game is paused.

How to pause all audio in Unity

By default, audio in Unity will continue to play even when the game is paused. This can be problematic if you want to completely pause the game, including audio. To pause all audio in Unity, you can set the AudioListener.pause property to true.

Here’s an example of how to pause all audio in Unity:

void PauseGame () { 
  Time.timeScale = 0f; 
  AudioListener.pause = true;
}
void ResumeGame () { 
  Time.timeScale = 1;
  AudioListener.pause = false;
}

In this example, we’re setting the AudioListener.pause property to true when the game is paused, and false when the game is resumed. This will pause all audio sources in the scene.

If you want to pause only certain audio sources in the scene, you can set the ignoreListenerPause property of the AudioSource component to true. This will prevent the audio source from being paused when AudioListener.pause is set to true.

Here’s an example of how to pause only certain audio sources:

void PauseSlimeAudio () { 
  myAudioSource.ignoreListenerPause = true;
}
void ResumeSlimeAudio () {
 myAudioSource.ignoreListenerPause = false;
}

In this example, we’re setting the ignoreListenerPause property of the myAudioSource component to true when the game is paused, and false when the game is resumed. This will prevent the slime audio source from being paused when the game is paused.

How to pause the game without using time scale

While setting the time scale to zero is the easiest way to pause a game in Unity, it’s not always the best solution. For example, if you have a state machine in your game, you may want to have all objects check against the state machine to see if the game is in a paused state, rather than relying on the time scale.

However, in most cases, using the time scale to pause the game is the best solution. The only times you might want to use an alternative solution are when you have very specific requirements, or when you’re using a third-party library that doesn’t play well with the time scale.

Summary

In this tutorial, we have learned how to pause a game in Unity using Time.timeScale. We also learned about what does and doesn’t get paused, and how to pause everything in the game except for certain objects using unscaled time.

We also learned how to use a coroutine when the game is paused, and how to animate menus and objects when the game is paused by changing the Update Mode of the Animator component to use Unscaled Time.

We covered how to prevent control input when the game is paused using the new Input System and how to pause all audio in Unity. Finally, we discussed how to pause the game without using Time.timeScale and why it might be necessary.

In summary, pausing a game in Unity is a simple yet crucial feature for many games. It allows players to take a break or adjust settings without losing progress. By following the steps outlined in this tutorial, you should be able to pause your game with ease and keep your players engaged.