esc key oddity

I am having an issue with my cursor not disappearing when I press the esc key. If I press escape in my game, it brings up the exit game menu. If I click no (which means I don’t want to exit the game) everything works as intended. The menu disappears the cursor disappears and the controls are turned on. However if I press escape with the quit menu up the menu disappears (like it should), the controls are enabled (like they should) but the cursor remains on the screen (it’s supposed to disappear).

It seems like every time the esc key is pressed the cursol.lockstate is set to None, regardless of what is in the code. I think I am missing something simple. Here is the relevant code:

	void Update () {
		if (pointerVisable == true) {
			Cursor.lockState = CursorLockMode.None;
			ShipControlsActive (false);
		} else {
			Cursor.lockState = CursorLockMode.Locked;
			ShipControlsActive (true);

		if (Input.GetKeyDown (KeyCode.Escape)) {
			if (quitting == false) {
				quitting = true;
				QuitGame ();
			} else {
				quitting = false;
				StopQuit ();
		}//end of esc key
	}//end of Update
	public void StopQuit(){
		ExitMenu.SetActive (false);
		pointerVisable = false;
    }//end stop quit
	public void QuitGame(){
		ExitMenu.SetActive (true);
		pointerVisable = true;
	}//end QuitGame

Thanks In advance
Richard M.

This could be caused while you are playing in the Editor since pressing Escape in the editor will always unlock the cursor, regardless of what you specify.

If this happens in the player, something else may be going on either inside ShipControlsActive() or with the pointerVisable variable.

As a side note, there’s no need to check and set the Cursor state every frame on the Update. Based on your code, you could simply lock/unlock the cursor in your QuitGame() and StopQuit() methods which will only run when the user presses a key.

@NicoVar Originally I did have it in the quit and Stop quit. I made a lot of changes trying to figure out the issue. Indeed it appears that the issue is not as bad in the player. I do notice that every second esc is not working.

For instance if I press escape and bring up the menu and use the “no” button to exit the next escape doesn’t bring the menu up, however the one after that does. I’ll take a look at the “extra code” that I didn’t post here, and start testing in the player.

Thanks for the assist.

Richard M.