- Home /
Screen.showCursor = !Screen.showCursor; Not working
I have Screen.showCursor set to false after the level is loaded(Not in this script). However I want the cursor to toggle each time input is received from the escape key. I know my Input.getKey stuff is working correctly because the GUI toggle works fine.
In case its not clear what I am trying to do. The user hits escape to bring up a menu--Then the mouse appears--They hit escape again and the cursor disappears! Not sure why my ! toggles aren't working :/
using UnityEngine;
using System.Collections;
public class GuiMenu : MonoBehaviour {
public bool menuShow = false;
public int menuX;
public int menuY;
void Update(){
if(Input.GetKeyDown("escape")){
menuShow = !menuShow;
Screen.showCursor = !Screen.showCursor;
Screen.lockCursor = !Screen.lockCursor;
}
}
void OnGUI(){
if(menuShow){
GUI.Box(new Rect(menuX, menuY, 100, 160), "Test Pause");
GUI.BeginGroup(new Rect(menuX, menuY, 100, 160));
if (GUI.Button(new Rect(10, 10, 80, 40), "Resume")){
menuShow = !menuShow;
Screen.showCursor = !Screen.showCursor;
Screen.lockCursor = !Screen.lockCursor;
}
GUI.Button(new Rect(10, 60, 80, 40), "Settings");
if (GUI.Button(new Rect(10, 110, 80, 40), "Quit")){
Application.Quit();
}
GUI.EndGroup();
}
}
}
did you check with actual Booleans? and one more thing keep
menuShow = !menuShow;
Screen.showCursor = !Screen.showCursor;
Screen.lockCursor = !Screen.lockCursor;
in a separate function, so that you can call it from Update and OnGUI, and use logs to check if you have any extra calls.
Answer by ExtremePowers · Nov 27, 2014 at 07:16 AM
I tend to use them like this
if(Input.GetKeyDown("escape")){
menuShow = !menuShow;
Screen.showCursor = menuShow;
Screen.lockCursor = !menuShow;
}
Just to make sure that the game creates a bug if one of them wasn't called.
Worked great! And makes more sense to do it this way. Thank you for the idea!
Your answer
![](https://koobas.hobune.stream/wayback/20220613172526im_/https://answers.unity.com/themes/thub/images/avi.jpg)