What am I doing wrong in this

I am a beginner and I’m trying to write a code to hide the cursor and lock it in place, but when I test it the cursor is hidden but not locked in place, please help

here is my code

#pragma strict

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

function DidShowCursor(){
	Debug.Log("Showing cursor");
}

function DidHideCursor(){
	Debug.Log("Hiding cursor");
}	

function DidUnlockCursor(){
	Debug.Log("Unlocking cursor");
	guiTexture.enabled = true;
}

function DidLockCursor(){
	Debug.Log("Locking cursor");
	guiTexture.enabled = false;
}	

function OnMouseDown (){
	//lock the cursor
	Screen.lockCursor = true;
	//hide the cursor
	Screen.showCursor = false;
}	

private var wasLocked = false;
private var wasShown = true;

function Update () {
	//if escape is pressed, unlock cursor and show cursor
	if (Input.GetButtonDown("Esc"))
		Screen.showCursor = true;
		Screen.lockCursor = false;
		
	if (!Screen.showCursor && wasShown){
		wasShown = true;
		DidShowCursor();
	}
	
	else if (Screen.showCursor && !wasShown){	
		wasShown = false;
		DidHideCursor();	
	}		
	
	if (!Screen.lockCursor && wasLocked){
		wasLocked = false;
		DidUnlockCursor();
	}		
	
	else if (Screen.lockCursor && !wasLocked) {
		wasLocked = true;
		DidLockCursor();
	}
}

I didn’t actually test your code, but by the looks of things you are missing some brackets on your first if statement, so that the cursor is always unlocked.
It should be,

if (Input.GetButtonDown("Esc")) {
   Screen.showCursor = true;
   Screen.lockCursor = false;
}