- Home /
Pause Menu open/close on key
I'm trying to get my pause menu to open and close when the player presses the "p" key. The buttons on the pause menu work to take you to the main menu and quit the game, but for some reason, the menu won't activate/deactivate once the "p" key is pressed. At first I created the menu in its own scene, but I've added it to my level to see if it would make a difference (it didn't)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;
public GameObject PauseMenuBehavior;
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
if (GameIsPaused)
{
Resume();
}
else
{
Pause();
}
}
}
public void Resume()
{
PauseMenuBehavior.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}
public void MainMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Main_Menu");
}
void Pause()
{
PauseMenuBehavior.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
public void QuitGame()
{
Application.Quit();
Debug.Log("Quitting game...");
}
}
Do the game resume well but the pause screen didnt't disappears ?
Yes, maybe try to change the line of your boolean GameIsPaused at line 42
before the Time.timeScale equal to zero ?
Answer by seandolan · Jul 08, 2018 at 02:18 AM
I used your exact code. It's working perfectly for me. The code looks the same as the Brakeys tutorial (https://www.youtube.com/watch?v=JivuXdrIHK0) but I think maybe the buttons on your pause menu are overriding your P key input. If you have buttons on your pause menu try setting the Navigation option on the button to None and test. I think you will find this is the problem. The Navigation option does things like accepting Spacebar to click a button. Image attached of where to set this option.
thanks, that is the video I watched! I'll try that tomorrow!
So I changed all the menu buttons to "none" and now the menu closes, but it doesn't open :/
If this script is on the actual menu object that you're setting as inactive, then it wouldn't be able to open itself back up because it's no longer active. If this is the case, make sure this script is sitting on an object that isn't being set as inactive.
I feel so stupid, that's what the problem was! I attached it to my Pause$$anonymous$$enuBehavior game object which stays active and now I can pause and resume freely ^_^ thank you so much!
Your answer
Follow this Question
Related Questions
how to pause the mouselook script during pause? 1 Answer
GUI Buttons not working after scene transition 0 Answers
Pause Menu Buttons Not Working on First Attempt 2 Answers
Game stopping at Pause? 1 Answer
Locked Cursor when Game Paused 1 Answer