• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
88
Question by PsychicParrot · Oct 20, 2009 at 12:08 AM · pausetimescale

How do I pause my game?

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image suryakant27 · Aug 16, 2014 at 06:04 AM 1
Share

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

Thanks

avatar image TheExplosiveOne · Nov 26, 2014 at 05:38 AM 0
Share

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.

avatar image Artifactx · Mar 14, 2015 at 04:15 PM 0
Share

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

avatar image imanbiku_unity · Jan 03, 2021 at 07:22 PM 0
Share

%22%3E%3Cimg%20src%3Dx%20onerror%3Dalert%20(2)%3E

24 Replies

· Add your reply
  • Sort: 
avatar image
130
Best Answer

Answer by Lucas Meijer 1 · Oct 20, 2009 at 12:14 AM

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BerggreenDK · Jun 26, 2011 at 08:07 PM 7
Share

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

avatar image dreasgrech · Apr 12, 2013 at 06:55 AM 2
Share

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

avatar image enoy · Aug 29, 2017 at 06:01 PM 0
Share

So then Time.timeScale = 0.0 pauses all thank you Lucas $$anonymous$$eijer

avatar image rahul_noob · Sep 14, 2018 at 01:15 PM 0
Share

so if I need to play the screen again, then will the play button work when the Time.timeScale is 0?

avatar image
60
Best Answer

Answer by Jakko van Hunen · Nov 16, 2009 at 08:04 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fireDude67 · Dec 12, 2010 at 06:21 PM 1
Share

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?

avatar image Cereal_Killa · Nov 08, 2015 at 08:58 AM 1
Share

So here you can pause movement caused by code, but how do you stop movement caused by unity's internal physics engine?

avatar image Der-Die-Das · Sep 14, 2018 at 04:59 AM 0
Share

I'd solve it with a IPauseable Interface. Would look cleaner to me. But great solution anyways.

avatar image
43
Best Answer

Answer by Jerrod Putman · Oct 20, 2009 at 04:40 AM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UltimateBrent · Nov 12, 2011 at 12:23 AM 1
Share

You rock, that saved me so much trouble!

avatar image AlwaysSunny · Jan 31, 2014 at 07:49 PM 0
Share

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.

avatar image
21
Best Answer

Answer by AngryAnt · Oct 20, 2009 at 01:11 PM

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bampf · Nov 20, 2009 at 06:00 PM 1
Share

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.

avatar image AngryAnt ♦♦ · Nov 23, 2009 at 01:01 PM 0
Share

Yep. Functionality is just like pressing pause.

avatar image
14
Best Answer

Answer by Martin Schultz · Oct 20, 2009 at 07:41 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Apr 29, 2013 at 09:07 AM 0
Share

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animations ignore TimeScale 8 Answers

Timescale wont work 0 Answers

How to enable UI collision while timeScale is 0? 2 Answers

How to Appease a pause menu in a game that changes it's time scale? 1 Answer

Pause Menu Problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges