How do I create a toggle for showing cursor and then hiding cursor?

Right now I have a button that will hid the cursor just fine but in order to have it appear I have to press the escape key. Here is what I thought would work for creating a toggle.

var toggle: boolean;


function Start () {
Screen.lockCursor = true;
}

function Update () {
if(Input.GetKeyDown("`")){
	if(toggle){
		Screen.lockCursor = false;
	}
	else{
		Screen.lockCursor = true;
	}

}

}

What am I doing wrong here and how do I fix it?

1 Answer

1

Oh, how dumb I have been. Ok kids remember when you put a boolean in that you want to keep track of a toggle be sure you change it’s value when you hit the toggle key. Here is my new code that works. I added a little extra as well.

var toggle: boolean;


function Start () {
Screen.lockCursor = true;
}

function Update () {
if(Input.GetKeyDown("`")){
	if(toggle){
		Screen.lockCursor = false;
		Screen.showCursor = true;

		toggle = false;
	}
	else{
		Screen.lockCursor = true;
		Screen.showCursor = false;

		toggle = true;
	}

}

}