Problems with UI buttons and Cursor position (or) UI buttons and instantiated canvas.

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.

Sometimes, you spend days trying to figure out a problem… looking in all the wrong places. Or ‘right places’ for people that actually have problems. Me… I’m just a victim of ‘idiot UI layout’.

It’s not surprising that no one responded to the above. The details to solve the issue weren’t available.

For anyone having similar problems… make sure you don’t have any UI components that are in front of your buttons. They tend to prevent the EventSystem from registering button clicks.

For me, I have an entire screen UI panel that flashed red when the character received damage. This was conveniently placed in front of all my buttons. A simple index move, and it all works.

For those that need to move indexes within C#, here’s the script I used (where ‘1’ is the number of positions it moved up):

        damageFlashIndex = instantiatedPlayerDamageFlash.transform.GetSiblingIndex();
        instantiatedPlayerDamageFlash.transform.SetSiblingIndex(damageFlashIndex - 1);