How do I relock my cursor?

I found this code online once and I’m trying to put in some lines that can relock the cursor but it isn’t working. Any help?

using UnityEngine;
using System.Collections;

public class CursorLock : MonoBehaviour {

    // A boolean to check if the cursor is locked.
        bool IsLocked;

    void Start () {
        // The mouse is invisible when the game is started.
            Cursor.lockState = CursorLockMode.Locked;
        // The cursor is locked.
            IsLocked = true;
    }
   
    void Update () {
        // To see the mouse again after pressing a certain key.
            if (IsLocked == true){
                if(Input.GetKeyDown("escape")){
                    // The cursor is freed!
                        Cursor.lockState = CursorLockMode.None;
                    // A boolean for relocking.
                        IsLocked = false;
                }
            }
           
        // To relock the cursor.
            if(IsLocked == false){
                if(Input.GetKeyDown("escape")){
                    // The cursor is locked!
                        Cursor.lockState = CursorLockMode.Locked;
                    // The boolean again.
                        IsLocked = true;
                }
            }
    }
}

It’s not working in what sense? To me, it looks broken (but maybe for a different reason).
maybe change your logic a bit

if(Input.GetKeyDown("escape")) {
   if(isLocked) { /* code here */ }
   else { /* other code here*/ }
   }

Currently, if it’s locked and you hit esc, it will unlock and then re-lock immediately :slight_smile:

I can unlock my cursor, but I can’t re-lock it. Btw I followed you advice and changed my code:

void Update () {
        // To see the mouse again after pressing a certain key.
            if (Input.GetKeyDown("escape")){
                if(IsLocked){
                    // The cursor is freed!
                        Cursor.lockState = CursorLockMode.None;
                    // A boolean for relocking.
                        IsLocked = false;
                }
                else {
                    // The cursor is locked!
                        Cursor.lockState = CursorLockMode.Locked;
                    // The boolean again.
                        IsLocked = true;
                }
            }
    }

Yet still. Same result…

Well, the code is better off nonetheless. Is this the entirety of the code? Just asking as I load up a simple scene to test this…

Oh, neat… I found what someone mentioned the other day in a similar thread. An editor issue with locking the Cursor!
That day, I thought it was working (but we were also hiding the cursor).
He suggested to build and test… I thought of that for your issue, because it didn’t work for me, either, but building the project did work :slight_smile:

Why not do it the same way Unity does in the standard assets?

        private void InternalLockUpdate()
        {
            if(Input.GetKeyUp(KeyCode.Escape))
            {
                m_cursorIsLocked = false;
            }
            else if(Input.GetMouseButtonUp(0))
            {
                m_cursorIsLocked = true;
            }

            if (m_cursorIsLocked)
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;
            }
            else if (!m_cursorIsLocked)
            {
                Cursor.lockState = CursorLockMode.None;
                Cursor.visible = true;
            }
        }

Didn’t

The code didn’t work for some reason.

Did you see my post about making a proper build to test it?
The code wouldn’t work for me in the editor, either, but did in a build.