- Home /
How to pause game.
I want to make a script where if P is pressed down the game will pause and show the gui. I know how to do the GUI part of it but I don't know how to pause.
Answer by raycosantana · Nov 18, 2013 at 05:38 PM
To pause use:
this is C#
if (Input.GetKeyDown("p")){
Time.timeScale =0;
}
For unpause use Time.timeScale = 1;
This has many interesting effect, for example if you use 0.5 instead of 1 you can slow down time.
$$anonymous$$ake sure u don't put this code in fixed update...
Answer by Sylafrs · Nov 19, 2013 at 04:23 PM
You can also use a static boolean ^^
You could need a menu that uses time to make some effects.. (for example a pause menu with a bar that goes up/down smoothly)
public static class Utils {
public static bool Pause = false;
}
// ...
public class PausableClass {
public void Update() {
if(!Utils.Pause) {
// Do some stuff
}
}
}
There's many cool other ways to do so : for example, Broadcast some messages such as "OnPause" and "OnResume"
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.BroadcastMessage.html
The time scale should be used to slow/accelerate time in order to make some cool effects. Not to pause the game, because its effect is global. You can also use it for Debug purposes..
either works really, personally i have a GUI state the tells the rest of the game when its paused.. which is more or less like this...
Answer by Pundek · May 29, 2014 at 08:20 PM
//That script works for me C#
public bool CanPause;
void Start () {
CanPause = true;
}
void Update () {
if (Input.GetKeyDown (KeyCode.Escape)) {
if(CanPause)
{
Debug.Log("pause");
Time.timeScale=0;
CanPause = false;
}
else
{
Time.timeScale=1;
CanPause=true;
}
}
}
Nope. Time scale doesn't really pauses your game. In a game you could use the time scale for other things. You could also need the time scale to be at 1 in your pause menu, so I think that's a bad idea :) A simple boolean must do the trick ^^
Your answer
Follow this Question
Related Questions
Pause code wont work 2 Answers
Time.timeScale doesn't stop void update() from beeing run 1 Answer
Pausing game by monitoring player script for start button being pressed 0 Answers
How to pause and unpause my game - Input.GetAxis doesn't work when Time.timeScale is 0 3 Answers
Two keys-Same function 1 Answer