Disabling Mouse Look with Escape wont work?

var CursorLocked = true;

function Update()
{
if (Input.GetKey(KeyCode.Escape))
    CursorLocked = false;
    
else
    CursorLocked = true;
   
    Screen.lockCursor = CursorLocked;
    
    if(CursorLocked == false) {
    GetComponent("MouseLook").enabled = false;
    //disable begge mouse look scripts af spilleren
    }
      if(CursorLocked == true) {
    GetComponent("MouseLook").enabled = true;
    //disable begge mouse look scripts af spilleren
    }
    
}

This is what I have got. It works, kind of… I’m trying to make it so if mouselook is false so is mouselook. Thing is, it only sets CursorLocked to false if you hold down Escape?

Side note: I have 2 mouselooks, one on camera one on player prefab, how would I get to the child camera of the player to disable it’s mouselook? I cant get this to work since I dont know what is going on and what to search on google. Any help appreciated, thanks.

Hey there. The way that code is structured CursorLocked will only be true while you are holding down escape, you can change this by using Input.GetKeyDown(), but there are other problems with that code as well, in your else you set CursorLocked to true then use the the value in an if which will always be true as you just set it. Try the code below it should make escape toggle the CursorLocked value on each down press and enable/disable your mouselook component.

var CursorLocked = true;

function Update()
{

if (Input.GetKeyDown(KeyCode.Escape))
{
    CursorLocked = !CursorLocked
}

if (CursorLocked == true)
{    
        Screen.lockCursor = true;
        GetComponent("MouseLook").enabled = false;
}
else {
    Screen.lockCursor = false;
    GetComponent("MouseLook").enabled = true;
}


}

Hope this helps.

Phill