Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
1
Question by PlanarBit · Mar 02, 2017 at 01:13 PM · unity 5monobehaviourpause gamepausing

Is there a way to programmatically pause the game (triggering all MonoBehaviour.OnApplicationPause() callbacks) ?

Hi,

Looking for the best way to pause the game.

Intuitively, I expected Application class to provide Pause() / Resume() methods to have MonoBehaviour-s paused / resumed (just like when hitting Editor's pause button).

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by UnityCoach · Mar 02, 2017 at 02:08 PM

OnApplicationPause is a message that a MonoBehaviour can use to handle when the application loses focus or is put in background.

Usually, to pause the game, you want to use a GameState enum, with a static property along with an event so that any component can subscribe to it, like :

     public enum STATE
     {
         Running,
         Paused,
         Over
     }
 
     public delegate void StateChange(STATE state) ;
 
     static public event StateChange StateChanged;
     static private STATE _state;
     static public STATE State
     {
         get { return _state; }
         set
         {
             if (value != _state)
             {
                 _state = value;
 
                 switch (_state)
                 {
                     case STATE.Running:
                         Time.timeScale = 1;
                         break;
                     case STATE.Paused:
                         Time.timeScale = 0;
                         break;
                     case STATE.Over:
                         Time.timeScale = 0;
                         break;
                     default:
                         break;
                 }
 
                 if (StateChanged != null)
                     StateChanged(_state);
             }
         }
     }
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 Shadoninja · Aug 07, 2018 at 03:55 PM 0
Share

Doesn't Time.timeScale also affect AudioSources? What if the player wants to pause without cutting off the music?

avatar image UnityCoach Shadoninja · Aug 07, 2018 at 06:36 PM 0
Share

No, Time.timeScale only affects the value returned by Time.deltaTime and Physics. That's why you always want to multiply translation and other transformations by Time.deltaTime, so that it's frame rate independent, and is affected by timeScale.

If you want to pause music, you need to handle this too.

avatar image Shadoninja UnityCoach · Aug 07, 2018 at 07:21 PM 0
Share

Hmmm.... I am still feeling like using Time to dictate pause state is only the proper solution for true "freeze the world" pausing scenarios. In situations where the game pauses while the player opens up a detailed menu system, it seems like that approach is too heavy-handed.

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

6 People are following this question.

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

Related Questions

Using interfaces and Monobehaviour behaviours 0 Answers

Can't move camera when scipt's attached to chosen gameobject 1 Answer

exception for pause 1 Answer

Unity Monodevelop Error - All Scripts are Broken 0 Answers

Is it possible to hooking Unity engine? -1 Answers


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