Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
0
Question by keeper-20 · May 08, 2012 at 12:11 PM · gamepause

time freeze (pause game)

With the following script press ESCAPE key and set time to 0 to freeze the game for pausing the game

when the ESCAPE key is pressed again the game should reset the game time to 1

that works in Unity editor but with the compiled game when I press ESCAPE it pauses when pressing the ESCAPE key again it doesn't reset, the game still pauses or is frozen

Why is that? It drives me NUTS because inside the Unity editor it works but NOT with the game outside Unity

function Update () 
{    
    if(Input.GetKeyDown("escape"))     
    {        
        if (Time.timeScale == 1.0)            
            Time.timeScale = 0.0;        
        else
            Time.timeScale = 1.0;                
    }
}

Comment
Add comment · Show 3
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 keeper-20 · May 08, 2012 at 12:13 PM 0
Share

function Update () {
if(Input.Get$$anonymous$$eyDown("escape"))
{
if (Time.timeScale == 1.0)
Time.timeScale = 0.0;

     else
                 
     Time.timeScale = 1.0;
                 
 }
 

}

avatar image tnetennba · May 08, 2012 at 12:46 PM 0
Share

I just tried your code and it works for me on a windows standalone using unity 3.5.1f2

avatar image keeper-20 · May 08, 2012 at 01:33 PM 0
Share

rearranging:

Screen.showCursor = false; Screen.lockCursor = true;

has for some odd reasons no effect

I also tried putting this into another script with no success

2 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by kolban · May 08, 2012 at 08:45 PM

When you set the Time.timeScale to zero, you have said that "time" should stop. This includes calls to the Update() function which is executed each frame update. But since time has stopped, there are no more frame updates. Using the OnGUI function callback is a place to be called when you still wish callbacks to occur even if time has stopped.

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 moghes · Feb 15, 2013 at 11:06 AM 0
Share

some important notes here by kolban, and this might be your answer. Check it and give credit!

avatar image d2clon · Feb 01, 2021 at 09:36 PM 0
Share

I am making tests and the Update function is still called even when Time.timeScale = 0. The one that is not called is FixedUpdate: https://docs.unity3d.com/ScriptReference/Time-timeScale.html

avatar image
0

Answer by aldonaletto · May 08, 2012 at 12:46 PM

Is this the whole Update code? It should work, but there are some pitfalls relative to setting Time.timeScale to 0 that may cause errors or infinite loops:
1- FixedUpdate isn't called anymore: if you're waiting for something to be altered in FixedUpdate, your program will get locked;
2- Time.deltaTime becomes zero, thus if you divide something by this value you will get an exception, and the rest of the function may not be executed.

Comment
Add comment · Show 5 · 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 keeper-20 · May 08, 2012 at 01:07 PM 1
Share

thanks for the fast reply

wanna ask you something more

I set the value now to 0.00001 that does the trick

but next problem is with this:

I have in the update function this:

function Update () {

 Screen.showCursor = false;
 Screen.lockCursor = true;
     
 if(Input.Get$$anonymous$$eyDown("escape"))     
 {
             
     if (Time.timeScale == 1.0)
     {            
         Time.timeScale = 0.00001;
         Screen.showCursor = true;
         Screen.lockCursor = false;
     }       
         
     else
     {
         Time.timeScale = 1.0;    
         Screen.showCursor = false;
         Screen.lockCursor = true;                    
     }
     
 }
 

}

I can't get it to work that the mouse pointer is showing when the game is paused and likewise when the ESCAPE key is pressed again that the mouse pointer is hidden again

From the script it should make sense what I like to do but can't get it to work

avatar image MangoDerp · May 08, 2012 at 01:24 PM 0
Share

Screen.showCursor = true; Screen.lockCursor = false;
Time.timeScale = 0.00001;

rearrang the order of the code.

avatar image flamy · May 08, 2012 at 01:31 PM 1
Share

code in onGUI will be called even on timescale 0 so you can use event to handle input for keyboard or buttons

avatar image keeper-20 · May 08, 2012 at 02:38 PM 0
Share

does that mean, it only works with on GUI function? because I tried that now and on GUI it works but not otherwise what I like to do is have some 3D buttons ins$$anonymous$$d of GUI stuff

any idea what I would need to do to get it to work?

avatar image aldonaletto · May 08, 2012 at 03:15 PM 0
Share

You should use only one of them, showCursor or lockCursor - at least in my PC using both hides the cursor forever.
Both properties hide the cursor, but showCursor = false do it only inside the game window - the cursor can be moved, and reappears when outside the window - while lockCursor = true hides and locks it in the middle of the screen.

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

11 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

Related Questions

How do I start game paused 2 Answers

How to display gameobjects when i pause the game? 1 Answer

how to pause and resume music after pausing game? 4 Answers

Game Pausing 1 Answer

End/Pause Game! 2 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