Unity Cursor changing back

I’ve problem using

Cursor.Lockstate
and
Cursor.visible

here is the full code:

 

       if (Input.GetKeyDown("e"))
        {

            InventoryStat = !InventoryStat;

            if (!InventoryStat)
            {
                Cursor.lockState = CursorLockMode.None;
                Cursor.lockState = CursorLockMode.Confined;
                Cursor.visible = true;
                INV.gameObject.SetActive(true);
            }
            else
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;
                INV.gameObject.SetActive(false);
                
            }
        }

When i press "e", the gui appears. but the mouse appears for one second and then instantly disappears. but when unlock the mouse with escape, it now wont disappear when pressing "e".
I have searched the entire internet for this problem but i cant find anything.

any help appreciated!

Unlocking the mouse with escape is editor only. So, if you’re manually changing the lock state, that will override it. You can’t expect the escape unlock to work in the built game, so test around it, not with it.