Screen.lockCursor not working

Sorry for my english
I have minecraft-clone game. I have a script that is hiding Pause(Panel) and Inventory(Panel too). I have some Screen.lockCursor in Pause hiding script. Screen.lockCursor isn’t working, I spent 5 days trying to figure out how to fix it, HELP ME PLEEEASE!!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class InventoryHide : MonoBehaviour
{
    public GameObject Inventory;
    public GameObject Pause;
    public float avgFrameRate;
    private float ElapsedTime_;
    [SerializeField] private Text FPSText;
    public bool lockCursor = true;
    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1;
        //Application.targetFrameRate = targetFrameRate;
        Screen.lockCursor = true;
        Pause.SetActive(false);

    }

    

    // Update is called once per frame
    void Update()
    {
        if(Pause.activeSelf == false)
        {
            if (Screen.lockCursor != lockCursor)
            {
                
                    Screen.lockCursor = true;
                
                
            }
        }

        if(Screen.lockCursor == true)
        {
            Debug.Log("true");
        }
        else
        {
            Debug.Log("false");
        }

        if(Input.GetMouseButton(1) || Input.GetMouseButton(0) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.Space))
        {
            Screen.lockCursor = true;
        } 
       
        ElapsedTime_+=Time.unscaledDeltaTime;
        if(ElapsedTime_ >= 1)
        {
            avgFrameRate = 1/Time.unscaledDeltaTime;
            ElapsedTime_ = 0;
            avgFrameRate = Mathf.Round(avgFrameRate);
        }
        /*Debug.Log(Mathf.Round(avgFrameRate));
        FPSText.text = "FPS: " + avgFrameRate.ToString();*/

        /*avgFrameRate = Time.frameCount / Mathf.Round(Time.timeSinceLevelLoad);
        
        if(Time.time >= 1.0f)
        {
            Time.time = 0;
            avgFrameRate = 0;
        }*/
        
        
        

        if(Input.GetKeyDown(KeyCode.E))
        {
            Inventory.SetActive(!Inventory.activeSelf);
        }
        if(Inventory.activeSelf == true)
        {
            //Cursor.visible = true;
            Screen.lockCursor = false;
        }
        if(Inventory.activeSelf == false)
        {
            //Cursor.visible = false;
            Screen.lockCursor = true;
        }

        
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            //SceneManager.LoadScene("mainmenu");
            if(Pause.activeSelf == true)
            {
                Pause.SetActive(false);
                Screen.lockCursor = true;
                Time.timeScale = 1;
            }
            else if(Pause.activeSelf == false)
            {
                Pause.SetActive(true);
                Screen.lockCursor = false;
                Time.timeScale = 1;
            }

        }
        

    }
}

Try

Cursor.LockState = CursorLockedMode.Locked

See if you can use that for what you’re trying to do.