Trouble Toggling Cursor On and Off in Unity

Hey, I’m currently trying to toggle the cursor on and off when reading and closing a note, as shown in the video.

However, once the cursor is enabled, I can’t seem to disable it again. Does anyone know why this is happening? Any help would be greatly appreciated.

Thanks in advance, and have a great New Year’s Eve! :heart:

Check with the debugger what the code is actually doing. The issue is probably not with the code you posted but rather it may get called with a different parameter again unexpectedly.

Do note that it is very bad style and prone to errors to write a conditional the way you did because you already tested if visible is true, you must not test again if it is false.

Always write such conditionals like so:

if (visible)
   //... 
else
   //...

But it can be simplified even more:

public void SetCursorVisible(bool visible)
{
    Cursor.visible = visible;
    Cursor.lockState = visible ? CursorLockMode.Confined : CursorLockMode.None;
}
1 Like