Locked Cursor when Game Paused

I’m brazillian, don’t look for error in my text :wink:

Hey Guys! I’m doing an 3D Isometric plataform game, and I’m having a problem with the pause menu. The Pause Menu works fine, but i want to when you enter in the menu, the cursor get unlocked and when you get out the menu, the cursor get locked again. This is the original pause menu code:

#pragma strict

var pause : boolean = false;
var pauseGUI : GUIText;
pauseGUI.enabled = false;

function Update(){
	if(Input.GetKeyUp(KeyCode.Escape)) {
		if(pause==true){
			pause = false;
		}
		else {
			pause = true;
		}
		if(pause == true) {
			Time.timeScale = 0.0;
			pauseGUI.enabled = true;
		}
		else {
			Time.timeScale = 1.0;
			pauseGUI.enabled = false;
		}
	} 
}

So i changed to this:

#pragma strict

var pause : boolean = false;
var pauseGUI : GUIText;
pauseGUI.enabled = false;

function Update(){
	if(Input.GetKeyUp(KeyCode.Escape)) {
		if(pause==true){
			pause = false;
		}
		else {
			pause = true;
		}
		if(pause == true) {
			Screen.lockCursor = false;
			Time.timeScale = 0.0;
			pauseGUI.enabled = true;
		}
		else {
			Time.timeScale = 1.0;
			pauseGUI.enabled = false;
			Screen.lockCursor = true;
		}
	} 
}

When I press ESCAPE, the menu open and the cursor is unlocked. But when i press ESCAPE again, the menu close and the cursor still unlocked! This is the ULTRA basic code I made to lock the cursor when open scene:

#pragma strict

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

function Update () {

}

Can someone help me? And explain why this happen? Thanks a lot!

Its a simple mistake you made.Take a closer look in the second code snippet.In both conditions you set ‘Screen.lockCursor=true’

if(pause == true) 
{
    Screen.lockCursor = true;   //You set this to true
    Time.timeScale = 0.0;
    pauseGUI.enabled = true;
        
}       
else 
{
    Time.timeScale = 1.0;
    pauseGUI.enabled = false;
    Screen.lockCursor = true; //Here also you set this to true
}

Change it to this

#pragma strict
 
var IsPaused : boolean = false;
var pauseGUI : GUIText;
pauseGUI.enabled = false;
 
function Update()
{
    if(Input.GetKeyUp(KeyCode.Escape)) 
    {
        //If game is already paused,resume it
        if(IsPaused)
        {
            //Set timescale to 1.0
            Time.timeScale = 1.0;
            //Disable GUI
            pauseGUI.enabled = false;
            //Lock the cursor
            Screen.lockCursor = true;

            //State the game is resumed
            IsPaused = false;
        }
        //Else pause the game
        else 
        {
            //Set timesclae to 0.0            
            Time.timeScale = 0.0;
            //Enable GUI
            pauseGUI.enabled = true;
            //Unlock the cursor
            Screen.lockCursor = false;

            //State the game is paused
            IsPaused = true;
        }
    }
}