- Home /
Pause Menu open/close with one key
Hello, I got a little problem here. I know why the code doesnt work, but i dont know how to get around this problem. I want to use the same button to open and to close my pause menu (escape). The problem is that when i set the first var (MenuOpen = true) my second if statement is true and it closes the menu again. How can i fix that.
The debug.logs where just there to test stuff out.
Here is the code:
var OpenMenu : boolean = false;
function Update() {
if (Input.GetKeyDown ("escape") && OpenMenu == false){
OpenMenu = true;
Debug.Log("Menu Open");
}
if (Input.GetKeyDown ("escape") && OpenMenu == true) {
OpenMenu = false;
Screen.showCursor = false;
Screen.lockCursor = true;
Debug.Log("Menu Closed");
}
}
function OnGUI () {
if (OpenMenu == true){
GUI.Box (Rect (Screen.width/2-125,Screen.height/2-200,250,300), "Pause Menu");
Screen.showCursor = true;
Screen.lockCursor = false;
Time.timeScale = 0;
Debug.Log("I got here");
}
}
Answer by sparkzbarca · Dec 27, 2013 at 01:27 PM
if (Input.GetKeyDown ("escape")){
if(!openmenu)
{ Debug.Log("Menu Open"); openmenu =true;
}
ELSE if(openmenu) //close menu;
} all you need is an else
Ahh ok thanks to both of you. I knew there was a easy solution. I still have a problem. $$anonymous$$y mouse also moves my caracter ($$anonymous$$ouseLookScript) can i freeze that with a command too?
@AW0610AUT - it would be better to post this as a new question. But of course it's possible to disable $$anonymous$$ouseLookScript after pressing a key, and enable it on the second press of this key:
var script : $$anonymous$$ouseLookScript = GetComponent($$anonymous$$ouseLookScript);
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
{
script.enabled = !script.enabled;
}
@sparkzbarca - I understand correcting an answer, when it contains a mistake. But seeing edit which totally changes the incorrect answer, copying details from another one which is correct, is just disappointing. If you see that your answer is incorrect, please delete it.
Answer by ArkaneX · Dec 27, 2013 at 01:29 PM
You can use if ... else:
if (Input.GetKeyDown(KeyCode.Escape))
{
if (!OpenMenu)
{
Debug.Log("Menu Open");
}
else
{
Screen.showCursor = false;
Screen.lockCursor = true;
Debug.Log("Menu Closed");
}
OpenMenu = !OpenMenu;
}
Answer by Frank126 · Dec 27, 2013 at 02:14 PM
var OpenMenu : boolean = false;
function Update() {
if (Input.GetKeyDown ("escape")){
if (OpenMenu){
OpenMenu = false;
Screen.showCursor = false;
Screen.lockCursor = true;
Debug.Log("Menu Closed");
}else{
OpenMenu = true;
Screen.showCursor = true;
Screen.lockCursor = false;
Time.timeScale = 0;
Debug.Log("Menu Opened");
}
}
}
function OnGUI () {
if (OpenMenu == true){
GUI.Box (Rect (Screen.width/2-125,Screen.height/2-200,250,300), "Pause Menu");
Debug.Log("I got here");
}
}
Note that you should never use the OnGUI() method to do logic since it is a drawing method that can be called more frequently then the Update() method (Source).
Ok thanks. I´ll cange my script a bit then. Thanks for the info.