Screen.CursorLock problems

Hey everyone!

I’m having a problem with hiding the cursor and locking it in the middle of the screen. It works at first but when I press esc it comes out of it and won’t go back in.

Here’s the code I am using to try and make this work :

void Update()
    {
        //Cursor Lock/Unlock//////////////////////////////////////////////////////////////////////////
        if (Input.GetKeyDown(KeyCode.Escape) && cursorUnlocked == false)
        {
            cursorUnlocked = true;

            Screen.showCursor = true;
            Screen.lockCursor = false;
        }

        if (Input.GetKeyDown(KeyCode.Escape) && cursorUnlocked == true)
        {
            cursorUnlocked = false;

            Screen.showCursor = false;
            Screen.lockCursor = true;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////
    }

Thanks for your help guys!

Im pretty sure that Screen.lock or Screen.showCursor has been removed since Unity 5.0. However, What I use is this: Unity - Scripting API: Cursor

So if you want to make the cursor visible/invisible, you can simply do:

Cursor.visible = true; // Or false

And to lock the cursor:

Cursor.lockState = CursorLockMode.Locked;

And to unlock it:

Cursor.lockState = CursorLockMode.None;

And take a look at Unity’s Screen class. Notice how there are not cursor variables:

Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;

this is the correct code.