- Home /
Lock Cursor problem in Unity 3D 5.
Lock Cursor problem in Unity 3D 5.
Hi there I have problem with Lock Cursor in Unity 3D 5 in C#
Replace " Screen.lockCursor = true; " on " Cursor.lockState = true; " and shows an error " error CS0029: Cannot implicitly convert type bool' to
UnityEngine.CursorLockMode' ". Tell me how to fix the error and how to move the cursor, and that he was in the middle of the screen. Need for the shooter.
Respond fast please !!!
Cheers !!!
Answer by maccabbe · Mar 02, 2015 at 11:09 AM
The error is telling you that Cursor.lockState is not a bool but an enum of type CursorLockMode. Looking in the unity documentation you can see that CursorLockMode can have 3 values, None, Locked, and Confined. Therefore use the following line
Cursor.lockState = CursorLockMode.Locked;
http://docs.unity3d.com/500/Documentation/ScriptReference/CursorLockMode.html
lockState doesn't exist for me, am I doing something wrong? (Probably :p)
Answer by elcast_cze · Jun 19, 2015 at 03:56 AM
https://www.youtube.com/watch?v=2bWoRTRT_DU
using UnityEngine;
using System.Collections;
public class HideLockCursorA : MonoBehaviour
{
void Start ()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update ()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
Answer by TexasFunk101 · May 17, 2015 at 06:31 PM
Here is what I did for my lock cursor script
function Start ()
{
Screen.lockCursor = true;
UnityEngine.Cursor.visable = false;
}
Screen.lockCursor can't be used anymore since it is obsolete.
Answer by Ziron999 · Sep 04, 2015 at 10:25 AM
You have to have it in update at this time due to a bug in unity where it loses focus. Hopefully they will fix this someday....Unfortunately currently the only way which is bad for performance is this way from what I can tell. You could detect and create an void OnFocus but that would be OS dependent :( In other words it would be different if it's a Mac/Phone/Windows/ect ect. So this is the only practical solution for when you lose focus(basically alt+tab) constantly.
void Update()
{
if (Cursor.lockState != CursorLockMode.Locked)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
I should add that it's still weird there is a bug with cursor.visable it will lock into the center but will not go invisible it may be best to not even use cursor.visable until unity dev's get this stuff straight...
Answer by Digestivbiscuitsm · Jul 29, 2017 at 10:42 AM
Well, the script that I used was this:
void Update()
{
//Mouse lock and confine
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
Cursor.lockState = CursorLockMode.Confined;
//and to unlock it is this:
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
I hope this helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220613184714im_/https://answers.unity.com/themes/thub/images/avi.jpg)