I created an UI button but click does not work.

Hi everybody, I’m trying to make a simple RestartGame Button for my game, the problem is that once I add the button I can’t click on It, it’s like it’s disabled, I wrote my script for the button and everything but the button does not work, no click, no mouseover effect, nothing. Any Ideas?

1 Like

For a button to work you need the following in your scene

  • A button that is a child of a canvas object
  • The canvas object must have a GraphicRaycaster component
  • There must be an EventSystem object somewhere in your hierarchy.

Unity will build all this automatically if you create your button from the GameObject menu. But not if you add a button component manually.

In addition to BoredMormon’s answer:
If you have multiple Canvases, you may need to specify the Sort Order for each (or some) in their Canvas components

For example, I had a HUD Canvas that’s always present, and a Paused Canvas that is active when the game is paused. I had to set the Paused Canvas SortOrder to be higher, so that it’s “in front” of the HUD. Otherwise, the raycast would hit the HUD Canvas first, and wouldn’t register with the Paused Canvas’s buttons.

Hi,
I don’t know why but my EventSystem was with the option Force Module Active unchecked, when I checked this option my button works.

@BoredMormon is correct, but if it is still not working, might I add that you also need to add a using statement to your script: using UnityEngine.EventSystem;

I recently encountered a loss of my working modal. I found that it was due to the mouse being locked by another script that made my click not actually fire on the button. Just a thought for anyone who might be looking.

I know this is an older question, but I ran into the same issue, but had a different reason for the fault.

Here is a solution to help you discover the issue.

  1. Basics: make sure the button is on the canvas, which must have a GraphicRaycaster and that event system is in the hierarchy. (I specify basics because this is the default setting.

**2. Debug: While the game is running in the IDE, select the Event System from the Scene Hierarchy. **

** a. At the bottom of the inspector is a debug output dedicated to explaining what the event system is doing.

b. Here you can see if your mouse is registering at all, if clicks are coming through, which objects they are triggering, and a lot more information…

I used this debugging ability to discover I had a transparent image that had raycast target enabled and was over it in the hierarchy.

Hi guys, I found another situation where Unity’s button script will fail to work as well: when your button has a parent and said button is positioned outside of the parent’s interactable area. Here’s an example:
67969-unity-screenshot.png

Suppose you have a dark blue panel and a white button childed to it. The button’s Button script works fine when it’s within its parent’s confines i.e. area outlined in green but won’t respond when moved out of its parent’s reach. This leads to 2 approaches : a) make sure button always stays within its parent’s area or b) don’t attach the button to any parent UI (except the Canvas) if said button has to be positioned outside the parent’s bounds.

Hope this helps :slight_smile:

make sure the camera is in the canvas if you made it render mode worldSpace

For anyone using that has assigned a panel as it’s own canvas, you need to add a graphic raycaster component to it or else it won’t register a button press. Both the button and the panel need their own raycasters in order to work.

I had my my Button component over Text component although button was in higher order it did not clicked.
So disabling the Text Raycast Target property was the solution that worked for me.

Also note that the EventSystem object has a second script called
StandaloneInputModule which expects four key bindings: Horizontal, Vertical, Submit, and Cancel. I had deleted all the key bindings because my VR app had no UI, but then added a debug button to trigger an event. It wouldn’t fire unless all four key bindings were added back to the Input Manager.

In my case, it was a text object that was raycasted.
If you have any text objects hovering above your buttons, make sure that Raycast is disabled in their Text component.

came to think about it after reading @SnStarr 's answer, thanks!,

If you create a custom button you can invoke the action configured in button component.
This code in inside the OnGUI method.

if (GUI.Button(new Rect(0,0, 100, 100), ""))
{
       Button button = GetComponent<Button>();
       button.onClick.Invoke();
}

I had the same problem, I’ve tried adding a new button from GameObject tab, still didn’t work. I’ve tried changing the layers, deleted and added EventSystems, nothing worked.

In the end I’ve closed Unity and opened it again and it worked! Strange…

I had the same problem, and what happened is that I had a noncritical error in the EVENSYSTEM (was lacking define an Input Value).

You might want to check your Event Camera (in the Canvas component on your Canvas) if this is not the camera which is active when you want to use the button, the event manager can’t see it.

I just found out why my button wasn’t working, and it’s completely different from the other problems. To debug a problem I always reduce it to the simplest possible. In this situation it worked. So back to my real program. If you highlight the Eventsystem while running the program it tells you what it is doing at the bottom of the Inspector. While running the game eligibleforclick field in the Eventsystem was false. So I dragged the mouse around and noticed that it was going false, true back and forth. BUT at the same time my camera was moving.

The problem was the camera script was hijacking the mouse cursor to move. It did not allow the button to activate.

The solution was to change the camera script to not use the mouse pointer and it worked. My knowledge of Unity is a mile wide and an inch deep. There may be a way for the camera and button to work together. I’ll have to work on the solution for my game. But this may help someone else.

Check that you don’t have a background on the same z position as the buttons! I had the same problem and couldn’t figure out what it was because I’d imported the buttons as part of a package from a game I made earlier and they’d worked fine, but it turned out it was just my background covering the buttons up, so just move the z pos of the background further away from the camera (assuming you’re in orthographic projection mode)

There’s 1 more thing!

1st) Follow all the requirements people said here;
2nd) The button REALLY has to be inside the canvas, not in any other UI component.
3rd) Don’t try to use that button you have created before, when it wasn’t working (looking like inactive), clicking from inside a panel or something and dragging to the canvas. Simply create a NEW button DIRECTLY inside the canvas.

This worked for me. None of the ideas left here had helped me until i figure this out…
Just a tip for the new and arriving guys like me…

Cheers!