- Home /
How to pause game when incoming call or home button pressed occur
Hello I’m having trouble pausing & resuming my game when the game is disturbed. I would like a countdown from 3 then resume. Here’s what I have so far
bool isPaused;
void Start ()
{
isPaused = false;
Time.timeScale = 1;
}
public void GamePause ()
{
isPaused = !isPaused;
if (isPaused)
Time.timeScale = 0;
if (isPaused)
Time.timeScale = 1;
}
void OnApplicationPause()
{
isPause = true;
Time.timeScale = 0;
}
I use accelerometer to move the player & this code pauses everything but the player & the game does not resume any help??
you forgot the bool parameter in OnApplicationPause. it tells you if the game pauses or resumes.
Answer by muhamad1987 · Oct 25, 2020 at 08:29 AM
@Stanley950 well first of all you have the if statements :
public void GamePause () {
isPaused = !isPaused;
if (isPaused)
Time.timeScale = 0;
if (isPaused)
Time.timeScale = 1;
}
you should change the second if to
if(!isPaused)
Time.timeScale = 1;
i would recomend instead of just returning the time to 1 , build a function that handels all the countdown and the release the pause function all together
for counting down you can use a basic courotine there is this video that explains pausing the game and counting down to return , just what you needed https://www.youtube.com/watch?v=9QwbCV9q348&t=235s