Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
1
Question by ayrton_adiong · Feb 02, 2014 at 06:19 AM · gameobjectpauseresume

How to pause and resume the game?

Hi there, I a new to unity, please helm me out with my problem. I am making a game called underwater Pressure. How can i pause my game and resume? That everytime i click the pause button the game will pause and there will be a pop up screen shows to resume the game or go back to main menu.

All i want is, everytime you click pause the game will pause and if i click resume, the game will resume where i pause. Please I need your help guys. Thank You So Much :)

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

3 Replies

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

Answer by supernat · Feb 02, 2014 at 06:38 AM

There is no special way to pause the game. You will just need to handle it as a game state no differently than if you are at the menu, mid game, showing the final score. You just need to plan to write your scripts so that they can all be paused. I typically check a global state in a manager script. So I have a Manager script for the entire game which is attached to a persistent GameObject. Then if someone clicks a GUI button for pausing, it issues the callback to the script for that GUI component which tells the manager to change state to Paused. Then all of my scripts check each frame during Update before moving through it. For physics based scripts, see this: http://answers.unity3d.com/questions/564000/can-i-pause-the-physics-simulation.html

By the way, this is a common question, so please search the forums and answers.

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 jg2115 · Feb 02, 2014 at 06:44 AM 0
Share

Sorry i answered at the same time you did, ill remove the answer becuase its pretty much the same.

avatar image CheesyMashedPotatoes · May 10, 2016 at 01:56 AM 0
Share

Hey, I just stumbled on this and was wondering how you would unpause it. If you send it to a PauseState, how would you return it to whatever state it was before?

avatar image tanoshimi CheesyMashedPotatoes · May 10, 2016 at 05:41 AM 0
Share
 void TogglePause () {
  gameIsPaused =! gameIsPaused;
 }
avatar image
5

Answer by NosVidz · May 10, 2016 at 05:35 AM

I prefer to use

 Time.timeScale = 0;

By setting the timeScaling factor to 0, you basically pause your game, since everything time-based will be stopped. Effectively, it results in Time.deltaTime to be constantly 0, and FixedUpdate is not called anymore. (Update and LateUpdate will still be called). If you want to have animations during pause, you have two possibilities:

  • Use an animator component with Update Mode: Unscaled Time.

  • If the animations are done through code (like rotation, simple movement or fading) you can use Time.unscaledDeltaTime. Like the name says, this is the unscaled delta value, and thus the same as deltaTime for timeScale = 1.

If you don't use a physics or time based game, you don't need to do anything in special. If your game is purely UI based, you can open a pause panel on click on the pause button. If the pause panel fills out the entire screen (can also be transparent, if you don't want it to look like that), it blocks every underlying UI input, and your game is effectively paused.

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 kamran-bigdely · Oct 13, 2017 at 07:47 PM 1
Share

Setting timeScale to zero causes the execution stops at the commands like this:

yield return new WaitForSecondsRealtime(3);

because that 3 seconds never passes.

avatar image
-1

Answer by sami1592 · Feb 02, 2014 at 07:32 AM

i was going to ask the same ques but because it is asked here i might as say it here.

i am facing problems to pause/resume my game as well. i am following lynda's unity 3.5 essential training. they did it with the function Input.ResetInputAxes();. but when i do the same movement of my mouse in game in X-axis freezes, but it doesn't do the same for Y-axis as shown in the tutorial :/

To freeze the objects in the game i used Time.timeScale = 0;

here is my code in game manager script --- #pragma strict

 var totalScore : int = 0;
 var temp : int = 0;
 var higestDistance = 0;
 var scoreInfoVariable : GUIText;
 
 var fpc :GameObject;
 internal var showMenu : boolean = false;
 
 var platform06 : GameObject;
 
 function Start () {
     Screen.showCursor = false;
 }
 
 function Update () {
     if (Input.GetKeyDown ("escape")){
         if (Screen.showCursor == false) {
             fpc.SendMessage("ToggleInputCursor", true);
             Screen.showCursor = true;
             showMenu = true;
             Time.timeScale = 0;
         }
         else {
             ResumeGame();
         }
     }
 }
 
 function UpdateScoreInfoGUI( distance : int) {
     totalScore = totalScore + distance;
     scoreInfoVariable.text = ""+ totalScore;
 }
 
 function ActivatePlatformPrefab06(){
     platform06.active = true;
     print("message is received");
 }
 
 function OnGUI(){
     if(showMenu == true)
     {
         GUI.BeginGroup(Rect(Screen.width/2 - 50, Screen.height/2 - 50, 100, 90));
         
         GUI.Box(Rect(0, 0, 100, 90), "Menu");
         if(GUI.Button(Rect(10, 30, 80, 20), "Resume")){
             print("you clicked  RESUME");
             ResumeGame();
         }
         if(GUI.Button(Rect(10, 60, 80, 20), "Quit")){
             print("you clicked  QUIT");
             Application.Quit();
         }
         
         GUI.EndGroup();
     }
 } 
 
 function ResumeGame(){
             Screen.showCursor = false;
             fpc.SendMessage("ToggleInputCursor", false);
             showMenu = false;
             Time.timeScale = 1.0;
 }

here is my code in First Person Controller script, my changes are shown with comments --- private var motor : CharacterMotor;

 internal var noInput : boolean;
 
 // Use this for initialization
 function Awake () {
     motor = GetComponent(CharacterMotor);
 }
 
 // Update is called once per frame
 function Update () {
 
     // -------------- my manual code-----------------
     if(noInput==true){ 
         Input.ResetInputAxes();
     }
     // -------------- my manual code-----------------
     
     
     // Get the input vector from kayboard or analog stick
     var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
     
     if (directionVector != Vector3.zero) {
         // Get the length of the directon vector and then normalize it
         // Dividing by the length is cheaper than normalizing when we already have the length anyway
         var directionLength = directionVector.magnitude;
         directionVector = directionVector / directionLength;
         
         // Make sure the length is no bigger than 1
         directionLength = Mathf.Min(1, directionLength);
         
         // Make the input vector more sensitive towards the extremes and less sensitive in the middle
         // This makes it easier to control slow speeds when using analog sticks
         directionLength = directionLength * directionLength;
         
         // Multiply the normalized direction vector by the modified length
         directionVector = directionVector * directionLength;
     }
     
     // Apply the direction to the CharacterMotor
     motor.inputMoveDirection = transform.rotation * directionVector;
     motor.inputJump = Input.GetButton("Jump");
 }
 
 // -------------- my manual code-----------------
 
 function ToggleInputCursor(state : boolean){
     noInput = state;
     print("message is received :"+noInput);
 }
 
 // -------------- my manual code-----------------
 
 // Require a character controller to be attached to the same game object
 @script RequireComponent (CharacterMotor)
 @script AddComponentMenu ("Character/FPS Input Controller")
 
Comment
Add comment · 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

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

23 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

Related Questions

Pause and resume coroutine 1 Answer

Can't set Time.timeScale back to 1. 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Best practice for countdown timer on game resume 1 Answer

Pause game-resume causing event to fire 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