- Home /
Trouble with unlocking cursor
I am having trouble with the cursor unlocking so that the user can click buttons. I have two objects using this same script. When the user presses E on the object, a panel will show with buttons. What should happen for both is that the cursor becomes unlocked so that they can click these buttons. This only works for one of these two objects, I can't figure out why.
Here's the code for the objects:
void Awake()
{
lockMode = CursorLockMode.Locked;
}
void Update()
{
var distanceToPlayer = (player.transform.position - transform.position).magnitude;
if (distanceToPlayer < 3)
{
AccessCheck();
}
if (CanAccess && (Input.GetKeyDown(KeyCode.E)))
{
Access();
}
if (OnPC)
{
lockMode = CursorLockMode.None;
}
if (OnPC && Input.GetKey(KeyCode.Tab))
{
Time.timeScale = 1;
currentScreen.gameObject.SetActive(false);
currentScreen = emptyScreen;
TerminalNum = 0;
screen = 0;
OnPC = false;
lockMode = CursorLockMode.Locked;
}
if (DisableA)
{
AlphaSpawner.gameObject.SetActive(false);
}
if (DisableB)
{
BravoSpawner.gameObject.SetActive(false);
}
Cursor.lockState = lockMode;
}
The variable OnPC is set in the Access function, if that's important. I really just don't understand what's wrong? Is it because the second objects are a prefab? Should they be entirely unique?
Answer by LukeThePunk666 · Jan 02, 2020 at 09:30 PM
Got it working in the end. Moved all references to locking the cursor to a GameManager script and it works exactly as intended now :)
Your answer
Follow this Question
Related Questions
Cursor won't stay locked in update!! 0 Answers
FPS Controller Mouse not locking consistantly 0 Answers
Cursor.lockState does not work 2 Answers
Issues with mouse input when locking the cursor 0 Answers
Alt+Tab pointer problem 0 Answers