"Escape" not working to open menu?

Hey guys! I have another (hopefully) easy question to answer. For some reason, the menu does not open when I press “Escape”, but closes when I press “Escape” and its already open. Anybody know why?

public bool isInMenu;
	
	// Update is called once per frame
	void Update () 
	{
	if(Input.GetKey(KeyCode.Escape) && isInMenu == false && NetworkManager.Instance.MyPlayer.IsAlive == true)
	{
	isInMenu = true;
	Debug.Log("Menu Opened");
	}
	if(Input.GetKey(KeyCode.Escape) && isInMenu == true && NetworkManager.Instance.MyPlayer.IsAlive == true)
	{
	isInMenu = false;
	Debug.Log("Menu Closed");
	}
	}
	
	void OnGUI()
	{
	if(isInMenu == true)
		{
		InGameMenu();
		}
	}
	
	void InGameMenu()
	{
	if(GUI.Button(new Rect(Screen.width * (0.1f/10f),Screen.height * (0.1f/6.3f),Screen.width/2, Screen.height/2), "Disconnect"))
		{
		Network.Disconnect();
		}
	}

1 Answer

1

Try this code and let me know if it works

if(Input.GetKey(KeyCode.Escape) &&  NetworkManager.Instance.MyPlayer.IsAlive == true)
{
  isInMenu = !isInMenu;
}

That should work as a toggle for the menu. My though would be that maybe the NetworkManager.Instance.MyPlayer.IsAlive might be false for some reason.

Works like a charm thank you :) Thank you very much!