- Home /
UI button not working
I'm doing a project and I created a UI canvas under which I added 3 empty GameObjects. In these empties, there are buttons. It worked properly before with no issue but then all the buttons in the 2 first empties stopped working. They react when you click them (by having a kind of gray tint) but they don't do call the function they're supposed to call.
I have - An event system
I've already tried : - Deleting and re-creating the event system - Add a Canvas group to the Main canvas GameObject - Add a Graphic Raycaster on the two empties with problematic buttons - Marking the buttons as interactable (they were already interactable anyway) - Restarting unity & playing the game (too many times)
The hierarchy of the UI :
MainCanvas
UIEmpty1
Button1
UIEmpty2
Button2
Empty
Button3
Button4
Button4
Button5
UIEmpty3
Button6
Button7
Also : They're not all open at the same time
If they are reacting to mouse hover and mouse press, the problem comes from your functions.
How have you added the callbacks? By code or from the editor?
Callbacks = functions called by the button upon click
Answer by Monsoonexe · May 21, 2021 at 05:56 PM
Since the Button reacts to being pushed visibly, this sounds like it could be a scripting error (an error in the code that the Button component calls onClick
).
Add some kind of test event to the problem Button's onClick
event in the Editor, something like en/disabling a Sphere that you can see or something. This will help you determine if it's a problem with the Button getting UI events (which sounds like is NOT the problem since it tints when pressed) or the code that is being called by the Button.
Are you rigging your event through the Editor or through a script? If you're rigging through a script, drop in some Debug.Log("someMessage");
to make sure the AddListener
code is working.
I tried that and it still didn't work. I defined keys on the keyboard and they work but the buttons don't. But, when I removed the lines for the buttons, it worked but instead of setting the boolean to true only when I press it, it sets it to true indefinitely.