I’ve got a bit of a unique problem, and am having trouble understanding the cause… let alone the solution.
I’m instantiating a canvas that is hidden until a player accesses their character stats. When they push ‘i’, the canvas becomes visible, and the cursor unlocks and becomes visible. However, buttons in this canvas do not work.
I’m using the FirstPersonControler that comes with Unity. Cursor visible/lock state is handled by this code:
if (Input.GetButtonDown("Inventory"))
{
lookCursor = !lookCursor;
}
if (lookCursor == true)
{
firstPersonController.enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
PlayerStats playerStatsscript = gameObject.GetComponent<PlayerStats>();
playerStatsscript.statsPanelOpen = true;
}
if (lookCursor == false)
{
firstPersonController.enabled = true;
Cursor.visible = false;
PlayerStats playerStatsscript = gameObject.GetComponent<PlayerStats>();
playerStatsscript.statsPanelOpen = false;
}
The statsPanelOpen simply alternates the Canvas SetActive bool.
If I run the game with the canvas already in the scene, the button works (the EventSystem is also preloaded in either case). However, if I instantiate the Canvas, and make the cursor visible and unlocked via the above code, buttons in the instantiated canvas and buttons that are in pre-existing canvases do not work. This leads me to believe that the cursor position isn’t being recognised by the EventSystem.
Instantiating the EventSystem after the Canvas does not help either.
Adding onClick Listeners and Delegates do not register either.
Any help is greatly appreciated.
Cheers,
Kris.
Edit: I’ve just tried removing the Cursor visible/locked codes, and used ‘esc’ to get a cursor visible with the Canvas open. Buttons did not work on the instantiated Canvas, but do on the pre-existing Canvas.