Cursor disappears when clicking, even after explicit instructions to not in Menu

What is supposed to happen :

Mouse cursor disappears while game is in play mode, reappears after pressing esc and toggling pause menu. While in pause menu, mouse cursor should remain active.

What happens:
Mouse cursor visibility does toggle, but not in correlation with pause menu/play mode. And while in pause menu, cursor does still disappear after clicking.

Note, this is in editor testing, not a build.

Here is my cursor lock state script:

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



namespace S3
{
    public class GameManagerToggleCursor : MonoBehaviour
    {
        GameManagerMaster gameManagerMaster;
        bool isCursorLocked = false;

        private void OnEnable()
        {
             SetInitialRefernces();
            gameManagerMaster = GetComponent<GameManagerMaster>();
            gameManagerMaster.MenuToggleEvent += ToggleCursorState;
            gameManagerMaster.InventoryUIToggleEvent += ToggleCursorState;
        }

        private void OnDisable()
        {
            gameManagerMaster.MenuToggleEvent -= ToggleCursorState;
            gameManagerMaster.InventoryUIToggleEvent -= ToggleCursorState;

        }

        private void Update()
        {
            CheckIfCursorShouldBeLocked();

        }

        void SetInitialRefernces()
        {
            gameManagerMaster = GetComponent<GameManagerMaster>();
        }

        void ToggleCursorState()
        {
            isCursorLocked = !isCursorLocked;    
        }

        void CheckIfCursorShouldBeLocked()
        {
            if (isCursorLocked)
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;
            }
            else
            {
                Cursor.lockState = CursorLockMode.None;
                Cursor.visible = false;
            }
        }



    }
}

Few things I have tried:

I thought the cursor behavior was tied to the standard asset character controller. I made an independent controller, but I guess the behavior is native to the editor because that made no difference.

I thought there might be a conflict between cursor.lockstate stuff happening in the camera control and the GameManagerToggleCursor (the above script), so I commented out all cursor.lockstate code in the camera control. No change.

Quick guess is that the issue is here. You’re setting visible to false either way.

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

That’s why I would prefer this notation:

Cursor.lockState = isCursorLocked ? CursorLockMode.Locked : CursorLockMode.None;
Cursor.visible = !isCursorLocked;
1 Like

@jvo3dc

I think you got it. I had been swapping things around trying to trial-and-error and must have messed that up.

The roles are reversed however. When in play the cursor remains, and while in menu it disappears. But that should be easy to fix…

@jvo3dc

great that did it. Everything working, I just had to adjust the default state of the bool isCursorLocked. thank you!

Thanks for sharing this different notation. I’ve seen something like this before but I am just starting out with coding so it seemed a bit confusing to me.

If you don’t mind, does this ```

  • Cursor.lockState = isCursorLocked ? CursorLockMode.Locked : CursorLockMode.None;
  • Cursor.visible = !isCursorLocked;

roughly translate to : 

cursor.lockstate = is this condition true ? yes : no
line 2. I don't understand this exactly.

logical negation of isCursorLocked… this is a Boolean we are talking about, so the logical negation would mean that whatever we evaluate, true or false, cursor.visible equals the opposite?

Yes, the first line is the ?: operator.

And the second line is just the logical negation.

thanks, I have been reading those references and trying to make my dumb monkey brain understand. It’s just not computing. Well, whatever it will make sense at some point. If not, I just memorize what to do for now. Thanks again,