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#
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#
Next time try Google: “unity pause game”
http://forum.unity3d.com/threads/18230-Pause-game
And the list goes on.
@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;
}
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.