Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Child objects of Button trigger the Button.onClick

Discussion in 'UGUI & TextMesh Pro' started by ISanchezV, Sep 25, 2019.

  1. ISanchezV

    ISanchezV

    Joined:
    Aug 26, 2019
    Posts:
    18
    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??
     
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    359
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,823
    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.
     
    dregan and markdavidmorris like this.
  4. ISanchezV

    ISanchezV

    Joined:
    Aug 26, 2019
    Posts:
    18
    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.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,823
    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
    1. If your children don't respond to input in the first place, then it doesn't matter whether they are interactable
    2. 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)
     
    MilenaRocha likes this.
  6. Crazsnowy

    Crazsnowy

    Joined:
    Oct 18, 2016
    Posts:
    1
    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.)
     
unityunity