Unlock mouse coursor to acces inventory

My issue is this one I try to acces my inventory to equip diferent items and I ususaly hit the “esc” key to show the mouse coursor and to unlock it, either way when I select the item I need the cursor locks again.

Here is the code I’m using, my unity version is Unity 5.6.1f1 64 bit

I’m using C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CursorUnlock : MonoBehaviour
{
 
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
 
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }
    }
 
}

Escape key only works when playing in editor I think. Anyway this seems fine so far… you just havent finished. Where is your UI code…

You could reference this script from your UI scripts if you want. Maybe make public void LockCursor() and public void UnlockCursor() functons. Because other scripts can’t call Start() and Update().