How to pause a game in Unity

The right way to pause a game in Unity

In Unity by John35 Comments

If you’re making a game in Unity then, chances are, you’re going to need a way to pause it.

This might be to stop the game to display a message, to access inventory or other game settings or to simply offer a traditional pause menu to the player.

Whatever it is, the benefits of being able to freeze gameplay temporarily will be obvious to you.

But, what’s the best way to do it?

Luckily, there’s a simple option.

So… what’s the best method for pausing the game in Unity?

The most convenient method for pausing the game in Unity is by setting the game’s time scale to zero (Time.timeScale = 0), which effectively pauses all time based operations including movement, physics and animation. Setting the time scale to one again will return the game to its normal speed.

In most cases just setting the time scale to zero to pause your game will be all you need to do.

However…

While most operations will be paused when using this method, not everything in the game will be affected.

In some cases, this is by design, for example when designing pause menus that still need to move and animate.

Other times, however, it’s possible to accidentally exclude in-game objects from being paused properly.

In this post, I’ll be exploring what is and is not affected by a time scale pause, how to exclude certain objects from being stopped as well as helping you to avoid some common pitfalls when pausing and unpausing your game.

Let’s get started.

What you’ll find in this article:

How to pause the game in Unity (using time scale)

To pause a game in Unity, simply set the time scale to zero to pause it and back to one (the default) to unpause it again.

In scripting it looks like this:

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

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

How does it work?

Setting the time scale affects the time and delta time measuring variables in the Time class.

Put simply, changing the time scale from its default of one will speed up or slow the game down – for example, you can run the game at half speed with a time scale of 0.5, or twice as fast with a timescale of 2). Setting it to zero, pauses the game entirely.

However, not everything is affected.

Understanding what actually happens when the game is paused using this method will help you to avoid unexpected behaviour later on.

Advertisement

What does and doesn’t get paused

Stopping the game using time scale will, effectively, pause every object that is time-based.

This typically accounts for everything that is happening in the game, as movement is usually scaled by delta time, animation, also, is time-based and physics steps will not be called when time scale is zero.

But not everything is stopped, and understanding what’s happening behind the scenes can help to make building a pause system a little bit easier.

Update will continue to be called

It may surprise you to know that Update will continue to be called when the game’s time scale is at zero.

This means that anything inside of an Update loop, including in coroutines, will continue to run regardless of the time scale, which can potentially cause an issue when detecting input (more on that later).

However, anything that relies on time-based measurement will be stopped, such as animations, time-based delays such as Invoke and Wait for Seconds and all movement, so long as it’s being multiplied by Time.deltaTime.

In practice, this means that, in most cases, everything will be stopped when the game is paused and, even though Update is still being called, nothing is actually happening.

FixedUpdate doesn’t get called at all

How to Pause Physics in Unity - visualisation of objects falling in the editor

When Time.timeScale is set to zero, Physics Steps (Fixed Update) will not be called.

When the time scale is set to zero, Fixed Update will not be called at all.

This is useful as it essentially freezes all physics-based movement automatically whenever the game is paused using the time scale method.

Advertisement

Time will be stopped

When the time scale is set to zero, Time.time, which is the time in seconds since the game started, will be stopped.

In most cases, this is a good thing, as any in-game measurement that relies on the Time.time value will also be paused with it.

But what if you still need to measure time when the game is paused?

Luckily there are a couple of options for doing just that.

Time.realtimeSinceStartup and Time.unscaledTime are not affected by time scale and will continue to measure time even when the game is paused.

Delta time based movement will be stopped

When the time scale is set to zero, all movement will be stopped, but only if that movement is being calculated in units per second (using Time.deltaTime).

What does that mean?

In Unity it’s good practice to multiply movement calculations by Time.deltaTime, which is the amount of time since the last frame. Like this:

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

This converts the rate of movement from units per frame to units per second.

It’s likely that you’re already familiar with this concept, as it’s used frequently when performing all kinds of actions over a period of time. The purpose being to maintain consistent, smooth movement, even when the frame rate changes (which it will).

But there’s another reason to use Time.deltaTime when moving objects.

When pausing the game using the time scale method, instead of the time since the last frame, Time.deltaTime will be zero.

This means that any movement that is being multiplied by Time.deltaTime, will be multiplied by zero. Which means no movement at all.

Usually, this is the desired behaviour when the game is paused.

If, however, you were to move an object using a fixed multiplier, like this:

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

In this example, the object would continue to move, even when the game is paused.

But what if you actually do want this behaviour?

For example, what if you want to pause the entire game, except for some objects.

What’s the right way to do that?

Advertisement

How to pause everything in the game except for certain objects

When pausing the game, you may want some objects to continue to move without being affected by the change in time scale.

Luckily, it’s possible to prevent objects from being paused by using unscaled time in place of regular time variables.

For example, to use unscaled time:

  • Instead of Time.deltaTime, use Time.unscaledDeltaTime
  • Instead of Time.fixedDeltaTime use Time.fixedUnscaledDeltaTime
  • Instead of Time.time, use Time.unscaledTime

Most variables in the Time class include an unscaled option that, when used, will ignore changes to the time scale.

Which is ideal for excluding certain objects from being paused, or from other time scale changes as well.

How to use a coroutine when the game is paused

Most coroutines will be frozen when the game is paused.

The coroutine itself isn’t actually suspended.

Instead, the timing elements of the coroutine, such as while loops, Time.deltaTime and WaitForSeconds, cannot complete and are preventing the coroutine from finishing.

But what if you want to use a coroutine when the game is paused?

Just like in the previous example, replacing Time.deltaTime with Time.unscaledDeltaTime will allow the coroutine to run on unscaled time.

There’s also a replacement for WaitForSeconds: WaitForSecondsRealTime, which will operate independent of time scale changes, allowing the coroutine to run as normal, even when the game is paused.

Advertisement

How to animate menus and objects when the game is paused

Animated pause message in Unity game

Use unscaled time to animate menus when the game is paused.

When building a pause menu, it’s likely that your menu screen, buttons and controls will all use animation.

There’s just one problem…

Animation is time-based and when pausing the game using the time scale method, any menu animations will be paused too.

Luckily there’s an easy solution, by changing the Update Mode of the Animator component to use Unscaled Time.

Unity Animations using Unscaled Time

Selecting Unscaled Time will prevent animations from being paused. Great for menus!

The unscaled time update mode ignores time scale changes and is ideal for animating pause menus and other GUIs.

How to prevent control input when the game is paused

While movement will be stopped when the game is paused, Update will continue to be called.

This can cause control input conditions (which are often placed inside the Update loop) to still be checked, even when the game is stopped.

In some cases, this isn’t a problem.

Either because the movement can’t occur or, in the case of physics-based movement, won’t be called at all, as Fixed Update is not called when time scale is at zero.

However…

Allowing gameplay input while the game is paused can cause you problems.

For example, in a top-down 2D game, it might be that player controls may still change the character sprite to face a different direction, even though the character isn’t moving.

Actions and other controls may also still fire, but may not work correctly or at all, only to then trigger when the game is unpaused again.

The controls that move the player, may also be required to navigate the pause menu.

In any case, for these reasons and more, it’s good practice to prevent gameplay input while the game is paused.

So what’s the best way to do it?

The simplest method is to first keep track of whether the game is paused or not with a public static boolean variable, like this:

public class PauseControl : MonoBehaviour
{
    public static bool gameIsPaused;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            gameIsPaused = !gameIsPaused;
            PauseGame();
        }
    }

    void PauseGame ()
    {
        if(gameIsPaused)
        {
            Time.timeScale = 0f;
        }
        else 
        {
            Time.timeScale = 1;
        }
    }
}

Making the variable static means that it is not specific to a single instance (the value is always the same, even if there are multiple instances) and that any class can access it.

Next, place all of the game’s input checks inside if conditions, so that they can only occur if the game is not paused, like this:

public class Jump : MonoBehaviour
{
    void Update()
    {
        if (!PauseControl.gameIsPaused)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                // Make the player jump!
            }
        }
        else
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                // Player can't jump! (The game is paused)
            }
        }
    }
}

Alternatively, you may want to use a more general condition, similar to the above, but that controls all of the gameplay input, turning it on or off. This would allow you to disable input when the game is paused and also for other purposes as well, such as cutscenes.

Adding this simple condition early on in development will make it easier to prevent unexpected behaviour later.

Advertisement

How to pause all audio in Unity

Audio Source in Unity game scene editor

Pausing the Audio Listener is the easiest way to pause all of the audio in a Unity game.

If you’ve tried using time scale to pause the game, you may have noticed that audio continues to play even when the game is paused.

The Audio DSP time value, which is the audio system time value used for Play Scheduled, will also continue despite the game being paused.

So how do you stop all of the game’s audio when the game is paused?

Helpfully there’s a very easy method for doing exactly that.

Pausing the Audio Listener will pause all audio in the game, as well as the audio DSP time value.

Simply set Audio Listener Pause to true when pausing the game, like this:

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

void ResumeGame ()
    {
        Time.timeScale = 1;
        AudioListener.pause = false;
    }

Audio that is paused using this method will resume from where it left off when the Audio Listener is unpaused again. Likewise, any audio that was scheduled to play using Play Scheduled will play at the correct time after gameplay is resumed.

But what about menu sounds and music?

What if you want to stop some sounds, but keep others playing?

Helpfully, you can instruct Audio Sources to ignore the Audio Listener Pause state, like this:

AudioSource.ignoreListenerPause=true;

This will allow special Audio Sources, such as for your menu and UI sounds to continue to play, even though other sounds have been stopped.

Advertisement

How to pause the game without using time scale

Setting time scale to zero is, in most cases, one of the most effective and convenient methods for pausing the game in Unity.

Despite this, I’ve seen a number of questions from people asking how to pause the game without using the time scale method.

This is, of course, absolutely fine. What works for one project may not be right for another.

There are options available for doing this, with the most straightforward being to simply check the game’s pause state before running any gameplay scripts. This is similar to the method of preventing control input mentioned earlier.

However…

When reading more about why some people would prefer not to use the time scale method, I was surprised to learn that most of the reasons why were related to not being able to animate menus, move objects, play audio or run coroutines.

Which, as you now know, are all possible to do when using the time scale method.

If it’s simply the case that you weren’t aware that the Audio Listener could be paused, or that animation could use unscaled time or that coroutines can actually be used when the game is paused, then the time scale method may still be the best option for you.

So give it a try.

Now I want to hear from you

How are you pausing your game? Are you using the time scale method or something else?

Did it work, and did anything happen that you didn’t expect?

And what do you know now about pausing a game in Unity that others could benefit from?

Whatever it is, leave a comment below and let me know.

Image Attribution

  • Unity Logo © Unity Technologies

by John Leonard French

Game audio professional and a keen amateur developer.

Get Helpful Game Development Tips, Straight to Your inbox

Get access to exclusive tips & tricks and master game development basics the easy way, with deep-dive tutorials and guides.

Popular Posts

Comments

  1. Great article!
    Thanks a lot.
    The reason I’m struggling right now with timescale=0 pause is custom shaders. They stop working. Pause menu itself is a bit complex and you can access a lot of “subscenes” form there with 3d objects that have custom shaders… Some UI items also have custom shaders.

    Workaround seems only one – to pass custom time to the shader.

    1. Couldn’t you simply use Time.unscaledDeltaTime in your custom shaders (that need to continue running code during pause)?

      1. Nice to see it all in one place, have been using all of this pretty much prior to reading this article, hadn’t thought to make the pause bool static as I have a game controller class and getter/setter functions for it from other classes (game objects) and already made my user input dependent on the state of this bool.

        Haven’t used ignore listener pause YET, as my end game menu isn’t that advanced at the minute, that’s next in my list!

        Shame the Unity documentation is so poor at the moment!

        Thanks for putting this all in one place, as it took me ages finding it all from online sources before seeing this, it WILL help others!

  2. I am trying to figure out how to hit the menu button during gameplay, have the game pause and the menu (which I have in another Scene) pop up, and when I want to close the menu and resume gameplay, not have the game restart. I currently have it pause the game using the Time.timeScale and then load the menu Scene. What I have a problem with is when clicking to resume the game scene, it starts the game scene over from the beginning.

    1. Author

      Depending on how complex your menu is, you may wish to try using a same-scene prefab instead, which might be easier to manage. This may or may not be the case, I don’t know your project. Expect to see a guide on this in the future.

  3. Thanks a lot! I paused my games using different work arounds and tripped on some problem, and always wondered which was the correct way of doing pause. Now, seeing your article I can be confident on what I am doing.

  4. This was very helpful, thank you. I’ve got my pause features working absolutely great, but I haven’t put in sound yet and I’ve just started doing animations and I was wondering how those were going to work with pause. Your information will let me move forward with total confidence. Thanks, again!

    1. One thing the article lacks is a description of the video player issues when pausing the game. In my experience, it continues to play.

      1. Author

        Interesting, I’ll take a look at that and update the article.

  5. Great article, thanks! I have a game where you give a force to a ball by dragging it (just like angry birds). I also have gravity involved in the game. When I pause the game using a button and disabling the main game empty object, I set a function to that button to set the Time.timescale to 0. In the pause menu I have a resume button that does the opposite: disables the pause menu and enables the main game “empty” with all the elements in it and set the time.timescale to 1. However, when the ball is in the air flying in an orb and I pause it and then resume it, the ball seems to fall in a straight line to the bottom and it doesn’t follow the orb anymore. Any ideas how to fix this?

    1. Author

      As I understand it, that shouldn’t happen. When the TimeScale is zero, physics steps aren’t called, including gravity. The only way I can imagine this would happen is if there’s a movement function happening in Update that isn’t scaled by delta time. Maybe try it on another object or in a different Unity version to troubleshoot.

  6. Many thanks for this – super useful. This is one of those bits of functionality that almost everyone needs, but (as far as I’m aware) hasn’t been covered that well – until now. (I tried to implement pausing on an old project several years ago but couldn’t find comprehensive advice, and ended up just plumping for a store asset instead (sadly now obsolete).

    But this seems to cover everything I need, so I’ve now got the function in my game in a few minutes – everything paused, but nice animated pause menus. Result! (From past experience I’d set aside a lot more time to get this working, so this is a massive bonus.)

    Thanks once again. 🙂

  7. Sweet rundown! Saved me a ton of time digging for info.

    To Pause videos you can do this:

    var videoPlayers = (VideoPlayer[])(FindObjectsOfType(typeof(VideoPlayer)));
    for (int i = 0; i < videoPlayers.Length; i++)
    {
    if (videoPlayers[i].isPlaying)
    {
    playingVideos.Add(videoPlayers[i]); // this is a List
    videoPlayers[i].Pause();
    }
    }

    And to Unpause:

    foreach (VideoPlayer v in playingVideos)
    {
    v.Play();
    }
    playingVideos.Clear();

  8. Great article! Learned a lot about how Time works in unity.

    I’m working on a simple game but want to a robust system to pause the game. Just to prototype –
    1. I have created a PausableBehaviour class which inherits from MonoBehaviour.
    2. A GamePauseController uses FindObjectsOfType(); to get list of all the PauseableBehaviours. This controller also exposes Pause() and Play() methods.
    3. When Pause() is called, it disables all the components that implement PauseableMonobehaviour and enables them again when Play() is called.

    This seems to work and gives flexibility in calling additional functionality when Pause and Play is called. For example, each behaviour can perform additional steps like stopping all co-routines or starting them.

    I don’t know how the system will scale with different types of games. So let me know if you foresee any issues that it may run into.

    1. Author

      Thanks for sharing your feedback. You asked if I could think of any issues with your approach: I’m certainly no expert, but my only thought would be to try to avoid manually starting / stopping behaviours unless you need to. For example, Coroutines, while their conditional loops will continue to be called during a timescale pause, nothing will actually change. So long as the conditions that progress the Coroutine are also paused or, in the case of movement, are scaled by delta time, it can’t advance until the game is unpaused. Your specific use case may be very different of course, my thinking is to avoid interrupting a behaviour that then won’t continue after the pause.

      All the best,
      John.

  9. First, thanks for this article. Very useful.

    I’m using the pause menu in a little 2D game I’m working on and so most of the subjects covered don’t apply to this particular game (but are good to know for future projects).

    One minor hiccup I had was that I use Time.TimeScale (TimeScale) to adjust the difficulty in the game. So TimeScale in my game is dependent on a SerializeField float (that’s adjustable in the inspector), and will possibly be adjustable in-game by users (haven’t decided yet). The problem that arose is that when I tried to set TimeScale directly for the pause feature, the change wouldn’t take. I had to make the change to my float field. Just want to mention this in case anyone else has had a similar problem.

    Secondly, I’ve nested my pauseGame code in my GameSession script. It seemed like a good place to have this code and I haven’t encountered any problems by doing this (so far). GameSession is mainly to persist the score from one level to another; it is kind of a Singleton and so there will only ever be one instance. And so I’m not sure my code requires the gameIsPaused bool to be static, but I’ve made it static just to be safe.

    Thanks again for this useful article.

  10. Holy crap in a pickle barrel, I’m so glad I signed up for these articles. I learned more in this one article than the entirety of my game programming ‘class’. Thank you for the great content, I’m looking forward to devouring much much more of it.

  11. Thank you so much …. This is very helpful and it clears most of my doubts on time scale

Leave a Comment