Locking and Unlocking a cursor...

Locking and Unlocking a cursor using the Tab key (to unlock) and clicking on the screen (to relock).

I have a short script I wrote to lock a cursor at the start of the game. However, with many failed attempts, I have tried to implement a button (Tab) that will unlock the cursor again. Can anyone help point me in the right direction?

Here is my code:

#pragma strict

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

function Update () {

	Screen.lockCursor = true;

}

if(Input.GetButtonDown("Tab")) {
	Screen.lockCursor = false;
}

Thanks in Advance!

The cursor is being locked every frame, therefore, when you try to unlock it, it is immediately locked again. Try this:

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

function Update() {
     if (Input.GetKeyDown("tab"))
          Screen.lockCursor = !Screen.lockCursor;
}

You write “button” but you use “Tab”. Here is code for the tab key:

function Start () {
    Screen.lockCursor = true;
}
 
function Update () {
	if(Input.GetKeyDown(KeyCode.Tab)) {
	    Screen.lockCursor = false;
	}
	else if (Input.GetKeyUp(KeyCode.Tab)) {
		Screen.lockCursor = true;
	}
}