Code acts differently in new project than in old project; Cursor.lockState issue
So I've updated to the latest version of Unity (2018.1.2f1) and obviously I had to update my project as well. After building it however, I've encountered an issue where allowing the game window to lose focus breaks the Cursor.lockState command. Once the window loses focus at least once, the game can no longer center the mouse until it the program is restarted. So, I decided to make a new project to demonstrate the issue and send it to Unity as a bug report. I typed the code, built the program and... It worked perfectly. In the new program I can click outside the window as much as I want, and when I lock the cursor again it does so properly. For testing I put the exact same code in my old project (Ensuring all other Cursor.lockState commands were disabled, so only the new script was running), and it still wouldn't work after building... So from what I can tell, Unity is compiling my old project differently than a new project... but I honestly don't really know what I'm talking about when it comes to this sort of stuff. Does anyone have any idea what's going and what I can do? This bug is a pretty annoying issue and I hope I can find a solution soon, but sadly I think I have no choice but to wait for Unity to get it fixed... Oh, and I think this issue might only occur on Windows.
Here's the code I used for testing btw:
public class CursorControllerScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.L))
{
Cursor.lockState = CursorLockMode.Locked;
} else if (Input.GetKeyDown(KeyCode.N))
{
Cursor.lockState = CursorLockMode.None;
}
}
}
Answer by mystman12 · Jun 04, 2018 at 02:38 AM
Found a solution. In the player settings, if "Run in background*" is disabled, this causes the bug. Enable this to fix the problem!