- Home /
How to Pause the game (Android) - correct way
Can someone tell me the the correct way to pause a game on Android? Currently what i am doing is just to set the Time.timeScale to 0 and AudioListener.pause to true; I do it in the game scene itself(Nothing hovers above the scene when paused). Code is give for reference
void pauseGame(){
paused = true;
Time.timeScale = 0f;
pauseGui.text = "[ resume ]";
AudioListener.pause = true;
Debug.Log("paused..");
}
void resumeGame(){
paused = false;
Time.timeScale = 1f;
pauseGui.text = "[ pause ]";
AudioListener.pause = false;
Debug.Log("resumed..");
}
The problem that I face is when I pause the game, lock my screen (Android) and then unlock it, the AudioListener gets re-enabled (don't know why).
I can use an Update() method to check and set AudioListener.pause to true if 'paused' boolean is true. But that will cause the background music to play for a split second. I know that there is some-other neat and correct way to pause the game, but don't know how. I am just starting to learn unity.
Can someone help me?
Answer by zharik86 · Sep 20, 2014 at 06:24 AM
The first in what function cause your pauseGame() and resumeGame()? Problem interesting, before the such I didn't note. There is a decision, but it not the absolutely. As you also told in Update() it is necessary to add a check condition. But thus in your functions to add one more line:
void pauseGame() {
paused = true;
Time.timeScale = 0f;
pauseGui.text = "[ resume ]";
AudioListener.pause = true;
AudioListener.volume = 0.0f;//set volume audio
Debug.Log("paused..");
}
void resumeGame() {
paused = false;
Time.timeScale = 1f;
pauseGui.text = "[ pause ]";
AudioListener.pause = false;
AudioListener.volume = 1.0; //set audio volume
Debug.Log("resumed..");
}
void Update() {
//Create condition
if(paused && !AudioListener.pause) {
AudioListener.pause = true;
}
}
I hope that it will help you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163336im_/https://answers.unity.com/themes/thub/images/avi.jpg)