- Home /
Pause Menu Not Pausing
I was wondering how do I stop processing game events once I hit the pause button? This is for android, I don't know if that makes a difference at all. Thanks in advance!
EDIT: I'm using C#
Answer by Vire · Jan 27, 2014 at 02:56 AM
Next time try Google: "unity pause game"
http://answers.unity3d.com/questions/7544/how-do-i-pause-my-game.html http://forum.unity3d.com/threads/18230-Pause-game http://answers.unity3d.com/questions/578156/how-to-pause-game.html http://answers.unity3d.com/questions/249886/time-freeze-pause-game.html
And the list goes on.
http://docs.unity3d.com/Documentation/ScriptReference/Time-timeScale.html
Answer by andrygames · Jun 19, 2016 at 08:30 PM
@markch25 Add this to the script after the click of the button:
private bool paused; //when the game is in pause this become "true"
private bool clicked; // when the button is clicked this become "true"
void Start ()
{
paused = false; // at the start, the game is not in pause
}
void Update () {
if (clicked == true)
{
paused = !paused; //if paused is true , will become false and the opposite
}
if (paused )
{
Time.timeScale = 0; //this will stop the game
}
else if (!paused )
{
Time.timeScale = 1;
}
Answer by Dibbie · Jun 19, 2016 at 07:18 AM
Using Time.timeScale you can control automated events that happen in your game, mostly control "Update" functions - for everything else (even using this, some stuff may not be paused, such as AI since that runs on a different system than "Update" at times), you could create a "master script", every other script in the game world will look at that in Update, or any time some type of movement or automated function should happen in your game, it will check this master script for the boolean "isPaused" or something similar, to determine weather or not to continue/carry out its action.
So that might look something like this:
void Update(){
if(Input.GetKeyDown(KeyCode.Escape)){
if(Time.timeScale == 1f){Time.timeScale = 0f;}//pause game
if(Time.timeScale == 0f){Time.timeScale = 1f;}//unpause game
}
Which would go on your object you want to trigger pause with - and instead of using the Escape key, you would use whatever method you want to control pause with - on Mobile "Escape" is registered as the phones "Back" button. You could also put this idea with OnFocus, which for a mobile game, may be better - so when they go to read a text message or something outside your game, their screen goes to sleep, etc - itll "pause" the game until they "resume" it.
The other way, is using a "master script" which would just be a script attached to your main camera or an object in the game world you never destroy and every script has easy access to reference, which would just have public bool isPaused = false;
then in pretty much every other script, youd surround your entire "Update" function, and any other automated functions with the reference, kind of like: (All code here is untested C#)
void Update(){ if(Camera.main.GetComponent<MasterScript>().isPaused == false){ //your Update code will run here... } }
and
//Example function... void FollowPlayer(Transform player, float speed){ if(Camera.main.GetComponent<MasterScript>().isPaused == false){ //your code here... } }
Since for example, "follow player" would be "automated" because the AI will never stop following the player until certain conditions are met, which may or may not be captured/paused with time.timeScale, at least from my experiences with using that method for pausing all the time.
Your answer
Follow this Question
Related Questions
Problems with pause menu displaying items and stopping player movement 2 Answers
How to make a pause menu in c#? 5 Answers
Zelda like pause menu 1 Answer
Creating Pause Game Menu 1 Answer
Trying to make sliding volume bars. 1 Answer