Child objects of Button trigger the Button.onClick

Hi,

I have a Panel with a Button component in the background, and then a series of panels, images et in front. However, all the panels, images, everything, trigger the Button in the background. I’ve already added Canvas Groups to everything (button, children of the button, main child of the button, etc) with every setting I can think of, I’ve set everything to Raycast Target, not Raycast Target, it doesn’t matter: the children images ALWAYS trigger the Button. There’s nothing adding an onClick to that panel, there’s nothing interacting with that panel at all (other than activating/deactivating it).

I’m working on 2018.4.9f1, in case that matters. How can I make it so that the UI elements children of the Button don’t trigger the Button??

This forum is for the UIElements framework. You’ll probably get better answers posting in the uGUI forum here:

That is just how Unity UI works; all child components feed UI events up to the parent unless they handle it themselves.

If you don’t want that to happen, I suggest you just change your object hierarchy so that those things aren’t children of the button. For instance, change your hierarchy from something like:

Button

  • Child 1
  • Child 2

to:

Empty Object

  • Button
  • Child 1
  • Child 2

Hypothetically, you could also give the children event handlers. E.g. if they have some component that already responds to pointer clicks, then they won’t pass those pointer clicks up to the parent.

2 Likes

Oops, sorry, I always get these things confused

I’ll do what you said with the hierarchy, I guess. My main problem is that using Canvas Groups is supposed to do what I want, and it’s not doing it. So I figure I’m doing something wrong.

I’m not sure how Canvas Groups would do what you want. A Canvas Group can disable an entire hierarchy at once (rather than needing to disable each thing individually), but

  • If your children don’t respond to input in the first place, then it doesn’t matter whether they are interactable
  • If you stop your children from being raycast targets, then clicks will “pass through” them to the button behind them, as if they weren’t there (which in this particular scenario gives the same net effect)
1 Like

I have the same problem,too.
Another possible cause is because your image’s rotation is180 (Item’s back doesn’t trigger raycast)
you can use scale = -1 instead of rotation 180
(Sorry I speak poor English.)

1 Like