Game doesn't start after death and pause menu
Hello, i've been working on a game for Android devices which includes a death and respawn system from a tutorial on youtube, the problem is after death i must press Esc to make the menu appear and after clicking on the Restart option it doesn't start until i press Esc again. Any help with that?
Manager script: public class Manager : MonoBehaviour { bool gameHasEnded = false;
public float restartDelay = 1f;
public void EndGame()
{
if(gameHasEnded == false && Lives.livesCounter == 0)
{
gameHasEnded = true;
Invoke("Restart", restartDelay);
}
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Pause menu script: public class PauseMenu : MonoBehaviour { public static bool gameIsPaused = false;
public GameObject pauseMenuUI;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (gameIsPaused)
{
Resume();
}
else
{
Pause();
}
}
}
public void Resume ()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
gameIsPaused = false;
}
public void Pause ()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
gameIsPaused = true;
}
}
Just a guess, but do you need to make gameHasEnded =false; again , maybe in the Restart(), or does that reset to false upon the game restarting? By hitting the escape the first time to get the menu to appear you are pausing the game so maybe thats why button doesn't work? and hitting it a second time to unpause it. Shouldn't pause$$anonymous$$enuUI.SetActive(true) appear automatically in the Restart()?
Answer by ZachCreates · May 03, 2020 at 11:16 PM
I may be wrong, but I believe your problem is caused by you setting Time.timeScale to 0. Since you did that, your restartDelay timer will never go down in time. The reason you need to press esc again is because when you do that, time starts up again. Do you need a restartDelay? If you took that out, it would be fine I believe.
I took the restartDelay and replaced it with Restart(); to summon the function but it does the same. It also keeps summoning prefabs like it isn't paused after death which i thought i made possible in another script:
public class $$anonymous$$anager : $$anonymous$$onoBehaviour { bool gameHasEnded = false;
public void EndGame()
{
if(gameHasEnded == false && Lives.livesCounter == 0)
{
gameHasEnded = true;
Restart();
}
}
public void Restart()
{
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name);
}
}
I’m not exactly sure what is happening here, but you could try debugging a little. Try adding these:
void Update() { Debug.Log(Time.timeScale); //Try debugging other things too! }
I’m sorry I couldn’t find the problem, but I hope that this helps!
Sorry for the delay, i've been busy with home office. I made a gif of the process i am having problems with:
I was using a website for gif edition and it have it's problems but i hope my problem can be understood. In less words when i die in game i need to press Esc, time keeps going, to show the restart menu, then click on restart to being again but i need to press Esc again to begin because for some reason the time remains in 0.
I’m not sure exactly what the issue is here. I think that it has something to do with changing the timeScale, but I don’t know. I have an alternative option that might work.
You could try making the restart function public, then putting the script as a component of the restart button, and finally linking the button being clicked to the restart function.
Your answer
Follow this Question
Related Questions
Need help with pause menu and cursor lock script conflict 1 Answer
hey im trying to make a pause menu, it keeps poping up at the start and i have no idea how to fix. 1 Answer
Need to press 3 times to pause in-game in Unity 0 Answers
Game doesnt start playing automatically after pressing restart. 0 Answers
Pause menu script help 0 Answers