Cursor.visible not working?
Hello, I am curious as to why I can't figure this one out. I have one scene acting as a main menu. I need the cursor to show on start, this here says it would work...
https://docs.unity3d.com/ScriptReference/Cursor-visible.html
void Start()
{
Cursor.visible = true;
}
That doesn't work, so I tried something like this instead.
public bool cursorOn;
void Start()
{
cursorOn = true;
}
void Update()
{
if(cursorOn == true)
{
ShowCursor();
}
else if(cursorOn == false)
{
HideCursor();
}
}
void ShowCursor
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
void HideCursor()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
Nothing I have tried after that has worked, so I am coming here to hopefully find it out. The only stuff I could find online was exactly what I tried, but it didn't work. It used to be so much easier Unity, why did you need to change this..?
How are you hiding the cursor to begin with? By default, the cursor should be visible.
I'm not hiding the cursor, it is gone on default for me.
$$anonymous$$ight be a bug, it shouldn't be hidden by default.
Do you have any other scripts that might hide the cursor? $$anonymous$$g. several camera controller scripts do that.
Answer by hilfygame-studio · Feb 20, 2020 at 02:08 PM
had same issue, just restarting unity worked for me (remember to safe),had same problem at me it was just restarting unity, no scripting issues
so short it would be:
public bool cursorOn;
void Update()
{
if(cursorOn)
{
ShowCursor();
}
else
{
HideCursor();
}
}
public void ShowCursor
{
Cursor.visible = true;
Cursor.lockState = CursorLock$$anonymous$$ode.None;
}
public void HideCursor()
{
Cursor.visible = false;
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
}
just check the cursorOn Bool in the inspector and it worked fine for me
Answer by adamstepinski · Oct 21, 2020 at 07:24 AM
in my case an external script on Camera executing every frame hid the cursor
Answer by bruceweir1 · Dec 03, 2021 at 07:10 PM
Here is the solution. Just apply this script to an active gameobject in your scene.
Note that in the Editor, the game frame needs to have focus straight after you press Play for this to work correctly (ie click on it straight after clicking the Play button)
public class LockCursor : MonoBehaviour
{
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void OnApplicationFocus(bool hasFocus)
{
if (hasFocus)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
}