UI Not Detecting Clicks, But Still Detecting Hover Events

I’m currently designing a game that utilizes a simple main menu that will eventually disappear to show the actual game view. I’m also using the new Input System, with a single Input Actions Asset with two Action Maps: one for the game and one for the menu.
On the UI, using UI toolkit, I’ve created a simple button with a hover psuedoclass to make it slightly larger. In a script, I’ve registered a simple click event onto the button to close the main screen on click:

The hover psuedoclass correctly works and applies the changes when hovering, but the Callback event is never called and the :active psuedoclass doesn’t apply when the button is clicked. Just very unsure on what to do, I’ve checked every forum and stack post I could find, and either the solutions weren’t applicable (two Input Action Assets, not OnEnable() callback registering, etc.) or they didn’t work (unified pointer, OnGUI() method call, etc.)

Buttons already have a delegate to hook into when they’re clicked. Why not just use that? Most likely it’s already intercepting click events and preventing further propagation.

I was pretty much unaware that this was already in place, at least for UI Toolkit. How exactly would you tap into this delegate within code, as that would definitely be easier

https://docs.unity3d.com/ScriptReference/UIElements.Button-clicked.html

2 Likes

It’s just a regular C# delegate: Unity - Scripting API: UIElements.Button.clicked

Get the button like you already are, and subscribe to the delegate rather than register a callback.

1 Like

That definitely seems to be a much easier way, however it still seems that it isn’t working. The hover action works fine, but even with subscribing to the delegate it doesn’t seem to be calling the given function when clicked

Well considering your overuse of ?. operators, and use of as, all this could just be silently failing on you.

1 Like

Did you verify that the button is actually resolved? Given UIDocument sets itself up in OnEnable as well, this could be some kind of execution ordering issue requiring you to set up in Start instead or change the script execution order.

First: please post code as text, and put it in code tags.

Remove UIDocument doc. You don’t need that. You only need access to rootVisualElement.
Replace your doc field with VisualElement rootElement. You assign it once:
rootElement = GetComponent<UIDocument>().rootVisualElement;

Then replace all doc.rootVisualElement uses with just rootElement.

I only added null propagation afterward, as I was afraid that may have been the problem, it doesn’t seem to be the problem. I’ll try casting it another way, but logging what startButton is produces the proper visual elements button, so I don’t think that’s the issue, just not sure.

I’ll check and see. The button seems to be resolving correctly when I debug it, but I’m not sure the OnClick function is ever being called regardless. It may very well be a problem with an incorrect action map assignment, but I haven’t seen any solutions as to how to resolve such

Understandable, at least for simplicity and efficiency. But I’m not sure any of that is actually the problem, though thank you for the coding advice in general

There’s a Q<T> overload, so there’s no need to cast anything.

Show an updated version of your code too.

1 Like

Which Unity and Input System versions are you using? Setup seems to vary depending on what you’re using.

You should be able to generally check events using the experimental UI Toolkit Event Debugger, which can be enabled at Project Settings/UI Toolkit/UI Toolkit/Advanced/Enable Event Debugger, then opened in Window/UI Toolkit/Event Debugger.

The Input System package comes with Packages/com.unity.inputsystem/InputSystem/Plugins/PlayerInput/DefaultInputActions.inputactions, which you can copy the UI action map from for a clean slate. On 2023.2 and greater, project-wide action map configuration is done in Project Settings. There’s an option to reset the Player and UI action maps there, and resetting both at once would mean that if you’ve modified the Player map, it’s probably safer to copy and paste that map to some new Input Action Asset beforehand.

2 Likes
private VisualElement rootElem;
    private Button startButton;

    private void OnEnable()
    {
        rootElem = GetComponent<UIDocument>().rootVisualElement;
        startButton = rootElem?.Q<Button>("StartButton");
        startButton.clicked += StartClicked;
    }

    private void StartClicked()
    {
        rootElem.Q("Background").visible = false;
    }

    private void OnGUI() {}

Was completely unaware of the overload, thanks for that. Seems to still not have done the trick though, sadly

Due to the project’s purpose, it’s being designed in LTS 2022.3.9f1, with Input System 1.7.0 9 (the latest release I believe). I did get the debugging window open, and it doesn’t seem to be sending the clicked action ever. And I previously reset the UI Input action map, it doesn’t seem to have worked. The UI actions don’t seem to ever properly register

My Solution At Least (very odd but it works)

So it seems I was completely wrong the first time I posted a solution, and that system nearly made things much more difficult. Simply creating a different action map asset for the UI, and then using that in the Input System UI Input Module seems to have worked, which is both odd and enraging, but if it works, it works. These two systems seem to work very poorly in conjuction together, which is so lovely

Many thanks to everyone who helped; even though that did’nt end up getting me the solution, it definitely helped, at least a bit, with code readability and efficiency.

The latest Input System release by publish date is 1.8.1 and is still specified to be supported under 2022.3 LTS. The page I had linked specifies that < 2023.2 requires using the Input System UI Input Module component, so you should see about configuring that if you haven’t already. If you have done so and still don’t see things working, maybe throw together a smaller project with a similar setup to see if that breaks. Aside from that, if possible for your uses, it is generally useful to update to the latest current 2022.3 release (2022.3.26f1 at 2024/4/23).

I’ll look into anything I can do concerning that, but I do seem to have found a solution at least to my problem, though thanks very much for the help. And I truly would love to, but I’ve been asked, for a myriad of reasons outside my knowledge, to use this version, and so I must. Thanks again though!

Unity 6, XRI, XRUIInputModule attached to EventSystem. Not working neither for mouse nor for controllers. Hover is ok, buttons are switched to active state but no clicks.
Complexity for complexity, unusable crap :expressionless: