- Home /
How to disable the escape key in a script
I have an issue regarding that I have a simple script connected to a game object that gets active when my player character losses all its hearts. Which makes a popup with "game over" and with two options to either retry or exit the game. The issue is that when the Game Over screen pops up, I am still able to press escape and hence my pause menu shows up in the background. I have tried to set the game object to not active when the game over object gets active, but this seems not to work. I hope you can assist. Best regards!
The code for the game over screen is pretty simple;
public class GameOverUI : MonoBehaviour
{
public void Quit()
{
Application.Quit();
}
public void Retry()
{
SceneManager.LoadScene("MainMenu");
}
}
Answer by Razor1994 · May 31, 2020 at 08:18 PM
There are 2 simple options here:
When you show the Game Over screen, you could disable the Script/Gameobject that listens for the Escape-Key. That way it would not trigger while being in the Game Over screen.
You could set a bool ("gameOverScreenActive") to true in the script that listens to the Escape-Key and when pressing the Escape-Key you could check whether that bool is true or false. If its false you show the menu. If its true you do not show the menu.
Hope this helps!
Answer by Aviryx · May 31, 2020 at 10:29 PM
You could use a boolean to check if the game is over wherever you are opening the pause menu.
public class GameManager : MonoBehaviour
{
public GameObject pauseMenu;
public GameObject gameOverScreen;
public bool gameOver;
if (Input.GetKeyDown(KeyCode.Escape)
{
if (!gameOver)
{
pauseMenu.SetActive(true);
}
}
public void GameOver()
{
gameOver = true;
gameOverScreen.SetActive(true);
}
}
Then invoke a method from another script (assuming you have a player script/manager that deals with player health etc)
public class PlayerManager : MonoBehaviour
{
public GameManager gameManager;
private void Dead()
{
gameManager.GameOver();
}
}
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
ApplicationLoadLevel Not working 1 Answer
What are the first steps to creating a 2D tile-based game? 0 Answers
Rotating a sprite distorts it? 0 Answers