- Home /
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; } }
function Update () {
if(Input.Get$$anonymous$$eyDown("escape"))
{
if (Time.timeScale == 1.0)
Time.timeScale = 0.0;
else
Time.timeScale = 1.0;
}
}
I just tried your code and it works for me on a windows standalone using unity 3.5.1f2
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
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.
some important notes here by kolban, and this might be your answer. Check it and give credit!
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
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.
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
Screen.showCursor = true; Screen.lockCursor = false;
Time.timeScale = 0.00001;
rearrang the order of the code.
code in onGUI will be called even on timescale 0 so you can use event to handle input for keyboard or buttons
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?
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
Follow this Question
Related Questions
How can I change my script to un-pause my project? 1 Answer
Pause Menu 3 Answers
How to Disable/Inactive NGUI Button on Game Pause? 0 Answers
Pause Menu Messes Up Game 1 Answer