How to make pause in unity

How to make pause in unity

How to pause a game in Unity

No matter what type of game you are making, pausing it will be needed at some point. Now 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 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

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 game is paused. You can set it back to one and the game will resume. Now let’s see what this does inside.

“Time.timescale” affects all physics systems in the game and all the movements 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 it’s transform inside an update function then the character will not freeze.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

Time.timescale doesn’t freeze the following

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. A Singleton if changed in one script 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”.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

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.

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. You can also check out more Unity tutorials here.

Pausing a game with Singleton

It’s much similar to how we reduced the player health. You need to define a static 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.

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.

Then you can simply set this variable to true to pause the game.

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. Let’s see what pauses and what doesn’t pause when Time.timescale is set to zero.

Things that get paused

Things That don’t pause

So, to efficiently pause a game do the following steps.

Revert the above steps to resume the game.

How to make a Pause menu in Unity

Hope you liked and understood the concept. If you have any query leave it in the comment below. You can also read the Unity manual for more details on timescale.

Volkan Ilbeyli

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

In this post, I’ll try to explain how to make a Pause Menu that pauses the game and displays a menu with various controls for this Minesweeper template example.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

Panel is the area where we’ll group the controls. First, we create a panel that will outline the layout of our menu.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

I strongly recommend watching Unity’s “Panes, Panels and Windows” training video if you want to grasp the new UI system that came with Unity 4.6.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

Quick recap: Here you can see the project hierarchy: on top is a UI_Manager GameObject that groups the whole controls that are contained in the Menu. Under UI_Manager GameObject, there is the UI_Canvas on which every UI element lives. UI elements are grouped with Panel s

I find it useful to create managers assigned for certain tasks that also interact with other managers. For example, I used:

Game Manager

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

Game manager script will handle toggling the Pause Menu and stopping the time using a public UIManager variable, which can be assigned from inspector by simply dragging UI_Manager GameObject onto the scripts variable field.

Music Manager

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

UI Manager

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

I find it handy to keep references of other managers in a manager as public objects. UIManager simply checks for keypresses in Update() function. If Esc is pressed, UIManagers calls GameManager’s public TogglePauseMenu() function. UIManager also defines the public functions to be called on Toggle and Slider Update events. These functions are later assigned in the related component from the inspsector.

Assign the Managers’ functions to Components

Now that we have defined the functions in Manager GameObjects that will be called when a certain event occurs, such as pressing the “Continue” button, it is time to assign them to the related components.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

There we go. We have a Pause Menu with functional music settings panel that you can adjust the volume of the music or mute/unmute it. Thank you for reading thus far! If you have any questions or feedback, feel free to comment below:)

Creating the Pause Menu in Unity

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

It is time to create the pause menu for my game.

To start, create a new button for the HUD panel. You can disable the text for this button. For the image, I found a nice pause icon for free in the unity asset store. Put this button at the top right of the screen.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

On the existing canvas create a new panel and call it the pause menu. You can copy the pause button and put it in the pause menu. Change the name to the resume button. Then add two buttons for the menu and to quit the game.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

Add the main menu script to the canvas. You can use the quit method for the quit button.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

In the script, get two references for the pause menu and the pause button. Then create new methods for the pause button, resume button, and menu button.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

Add the references to the script.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

For the references in the main menu, you can drag the pause icon into the hierarchy and disable it. Then add that to the references.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

Now add the methods to the correct buttons.

Miguel Angel Yee Rebollar

Read more posts by this author.

Miguel Angel Yee Rebollar

Having trouble creating a Unity pause menu? GameDev.tv Unity student and forum Master Problem Solver, Miguel, is here to help!

Creating a Pause menu is one of those things I didn’t know how complicated it was until I tried it, many claim it’s really simple, but I disagree, specially in 2D games.

As you have probably seen all over the internet, to pause a game you only need to set Time.timeScale to 0, which in theory is right, so, let’s try that and see how everything goes. I already have a small game with a character that shall be named Stickman Rick. Stickman Rick can move, jump and turn into Super Stickman Rick with the press of a button (I know that’s «op», let the Game Designer deal with that).

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unityStickman Rick running wild in the plains.

I want to give the player the ability to pause the game when pressing the escape key.

Wait! Can you sense it? It’s time. for a Challenge!

In the spirit of the GameDev.tv courses, I’ll be challenging you throughout this tutorial. Here you can download the project I’m using so you can follow along. Create a new Unity Project and simply drag and drop the package.

Hints

The code should look similar to this:

This will surely pause Stickman Rick’s endeavours, but this won’t help him get back to his task. The player should be able to unpause the game, for that we need to implement an «if» statement, or even better, a Ternary Operator.

1) Value to set, 2)Condition, 3) Value if condition equals true, 4) Value if condition equals false.

The Ternary Operator behaves as an «if» statement but written in a single line.

Usually a game has a Pause Menu, the project already has one, we only need to activate or deactivate accordingly.

First; I don’t like that Mathf.Epsilon, let’s change that to a bool that serves as a state, that way we’ll be able to have more control.

Challenge Time and Hints!

In the «PauseSystem» script:

This definitely works like a pause system, but in reality, it’s not working yet, there are 3 bugs that need our attention. Did you find any?

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unityPressing the Jump key multiple times while the game is Paused makes Stickman Rick fly. How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unityYou can turn into Super Stickman Rick even when the game is Paused! That’s OP!

Why does this happen?

Time Scale does not slow execution, this means that your code is still running. When the player presses the jump key, the code will apply a force to Stickman Rick, if the game is paused and the player presses the jump key it will still apply the force, press the button multiple times and the code will apply a force the amount of times the key was pressed making Stickman Rick fly when unpausing the game.

The Super Stickman Rick animation plays because when you set an animation without a transition or exit time it will transition to the next state if the conditions are met regardless of the Time Scale. This is a very common issue in 2D games.

To fix this we need to know if the game is paused and prevent the input from occurring. In our «PauseSystem» script we need a new function that returns the value of our bool «isPaused».

We need a way to access in our Stickman Rick controller the function we just created and then prevent the input from happening if the game is paused.

We are almost done. There’s a third issue with the pause system we need to fix before we call it a day.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unityStickman Rick showing his moves in the Pause Menu. This cannot happen if the Time Scale is set to 0.

If you need animations to play when the Time Scale has been set to 0 you only need to change a property in the animator component.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

Set the UI Animator Controller’s Update Mode to Unscaled time. That’s it! Stickman Rick is ready to show his moves even when the game is paused!

Creating a Pause system can be a little intimidating and confusing, just try to remember the following tips;

Hope you liked this small tutorial.

You can download the finished project here.

Game Dev Beginner

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

The right way to pause a game in Unity

In Unity by John French February 13, 2020 86 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.

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:

Pausing in Unity: Overview video

For a general overview of how to pause in Unity, try my video, or continue to the full article below.

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:

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.

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 make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

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.

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:

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:

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?

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:

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.

How to animate menus and objects when the game is paused

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

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.

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

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.

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:

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:

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.

How to pause all audio in Unity

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

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:

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:

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.

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.

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

How to make pause in unity. Смотреть фото How to make pause in unity. Смотреть картинку How to make pause in unity. Картинка про How to make pause in unity. Фото How to make pause in unity

by John Leonard French

Game audio professional and a keen amateur developer.

Get Game Development Tips, Straight to Your inbox

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

My favourite time-saving Unity assets

Rewired (the best input management system)

Rewired is an input management asset that extends Unity’s default input system, the Input Manager, adding much needed improvements and support for modern devices. Put simply, it’s much more advanced than the default Input Manager and more reliable than Unity’s new Input System. When I tested both systems, I found Rewired to be surprisingly easy to use and fully featured, so I can understand why everyone loves it.

DOTween Pro (should be built into Unity)

An asset so useful, it should already be built into Unity. Except it’s not. DOTween Pro is an animation and timing tool that allows you to animate anything in Unity. You can move, fade, scale, rotate without writing Coroutines or Lerp functions.

Easy Save (there’s no reason not to use it)

Easy Save makes managing game saves and file serialization extremely easy in Unity. So much so that, for the time it would take to build a save system, vs the cost of buying Easy Save, I don’t recommend making your own save system since Easy Save already exists.

Comments

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.

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

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!

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.

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.

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.

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!

You’re welcome! Glad it helped.

Very useful! All in one place.

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

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

Have you had a chance to look into this? I don’t think I’ll even be using video pausing in my game, so probably not a big deal. I just saw this in the comments and thought I’d follow up on Sergey’s question for others that come here looking.

Thanks for the article! I had no idea how to do a pause menu until now. This is my first step in learning it, and what a perfect step it is. Feels like going from 0 to 60 in no time at all.

Hi William, no sorry, I haven’t. I usually keep a list of extra items and questions to update articles in the future but this one seems to have slipped through the cracks. Sorry about that.

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?

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.

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. 🙂

Thanks! Really great to hear that it was useful.

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

Thanks for the tip.

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.

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.

This was helpful to me. Thanks!

Really great article, thanks man!

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.

You’re welcome! Thanks for sharing your experience with this too.

Very useful. …..
Thankful.

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.

Really glad it helped. Thank you!

Thank you for the guide. I appriciate it.

Thank you man. It was so good

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

No problem, glad it helped.

Thank you Sir.
Awesome Covered So many things…

I forget this code but u remember me this code thank u.. :).

This was exactly what I needed trying to write a pauseManager. Big thanks from Sweden.

Signing up for the newsletter.

Really glad to hear it helped!

Great article! Thank you very much!
But still I have some issues. Is it possible to not pause physics for some objects? I making a VR game, where UI consist of physical interactive objects. So when game pauses, I need to freeze “world” objects interactions (enemies, bonuses, environment etc.), but Pause menu needs to stay interactive and simulated.

Thanks! to answer your question, off the top of my head, I’m not aware of a way to selectively pause some physics objects but not others so it’s possible you’ll need some kind of workaround for this.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *