How do I pause my game?

What code do I need to make my game pause? How do I stop everything?

more ▼

asked Oct 20, 2009 at 12:08 AM

avatar image

915 9 22 21

Time.timeScale = 0.0 worked for me, however to resume game functioning I have to set it back to 1.0.


Aug 16, 2014 at 06:04 AM suryakant27

1 problem though, if paused using this method, all velocity of characters is lost. As soon as 1 pause it using this method, when I resume, the character no longer has the velocity it had before the pause.

Nov 26, 2014 at 05:38 AM TheExplosiveOne

Here is a tutorial that teaches you how to do that: http://youtu.be/z5oA-DvVGoo

Mar 14 at 04:15 PM Artifactx
(comments are locked)
10|3000 characters needed characters left

18 answers: sort voted first

In the Editor, you can just click the pause button.

From gamecode, you can set Time.timeScale to 0. See the help entry. This should pause most of your game, assuming you don't rely on stuff like asking the actual system time. You can also set timeScale to anything between 0 and 1 (and even 1 and up), to modify the speed at which your gamesimulation progresses.

more ▼

answered Oct 20, 2009 at 12:14 AM

avatar image

Lucas Meijer 1 ♦♦
9.6k 33 99 140

will this have any influence on any sounds/music playing if you scale the speed?

Jun 26, 2011 at 08:07 PM BerggreenDK

@BerggreenDK: No, but you can manually reduce the pitch of your music to match the timeScale.

Apr 12, 2013 at 06:55 AM dreasgrech

Just great and solve my problem. thanks

Sep 07, 2014 at 07:56 PM faraz

Worked a treat, thanks!

Jan 03 at 02:17 PM SirTwill
(comments are locked)
10|3000 characters needed characters left

If you rely on the time to remain active during the pause you can also implement it a following way.

If an MonoBehaviour needs a pause action, like e.g. stopping the animation, implement an OnPauseGame() function. To handle resuming, implement an OnResumeGame() function.

When the game needs to pause, call the OnPauseGame function on all objects using something like this:

Object[] objects = FindObjectsOfType (typeof(GameObject));
foreach (GameObject go in objects) {
    go.SendMessage ("OnPauseGame", SendMessageOptions.DontRequireReceiver);

And to resume call OnResumeGame on all objects.

A basic script with movement in the Update() could have something like this:

protected bool paused;

void OnPauseGame () { paused = true; }

void OnResumeGame () { paused = false; }

void Update () { if (!paused) { // do movement } }

A big benefit of doing it like this is that you can implement object specific pausing and resuming functionality, like storing and restoring information for objects that use physics.

more ▼

answered Nov 16, 2009 at 08:04 PM

avatar image

Jakko van Hunen
604 2 2 5

Does that really pause the game? Or just send a message to all GameObjects that they should pause, but does not set Time.timeScale or something like it to 0?

Dec 12, 2010 at 06:21 PM fireDude67
(comments are locked)
10|3000 characters needed characters left

If you set Time.timeScale to 0, yet you still want to do some processing (say, for animating pause menus), remember that Time.realtimeSinceStartup is not affected by Time.timeScale. You could effectively set up your own "deltaTime" in your animated menus by subtracting the previous recorded Time.realtimeSinceStartup from the current one.

more ▼

answered Oct 20, 2009 at 04:40 AM

avatar image

Jerrod Putman
597 5 8 12

You rock, that saved me so much trouble!

Nov 12, 2011 at 12:23 AM UltimateBrent

I dont understand how would you do this.

Aug 29, 2013 at 03:03 AM Croirefly

Response to question in previous comment: Just keep a private float called lastRealtimeSinceStartup, and set it equal to Time.realtimeSinceStartup at the end of the script's Update() method. Anywhere else in Update(), subtracting one from the other will give you the effective deltaTime, even when Time.timeScale is zero.

Jan 31, 2014 at 07:49 PM AlwaysSunny
(comments are locked)
10|3000 characters needed characters left

If you're in the editor, you also have the option of calling Debug.Break to pause execution. This works just like pressing the pause button and is ignored at runtime, so you could use it to make debug-time "breakpoints".

more ▼

answered Oct 20, 2009 at 01:11 PM

avatar image

AngryAnt ♦♦
4.6k 21 37 89

Programmers should note that Debug.Break does not act like a true breakpoint. Execution is not paused on that line, but rather, the game stops after the current frame has finished processing.

Nov 20, 2009 at 06:00 PM Bampf

Yep. Functionality is just like pressing pause.

Nov 23, 2009 at 01:01 PM AngryAnt ♦♦
(comments are locked)
10|3000 characters needed characters left

Setting Time.timeScale to 0.0 works great as you still can have at the same time a Unity GUI on top that is still clickable and the rest of Unity freezes meanwhile.

more ▼

answered Oct 20, 2009 at 07:41 AM

avatar image

Martin Schultz
1.9k 23 63 130

it's a great point that UnityGUI does indeed keep going when timescale is zero. (But it can cut both ways, be careful! :) )

Apr 29, 2013 at 09:07 AM Fattie
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 20, 2009 at 12:08 AM

Seen: 167480 times

Last Updated: Mar 14 at 04:15 PM