Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Canvas group without "block raycast", but the button should be enabled

Discussion in 'UGUI & TextMesh Pro' started by rapidrunner, Jul 29, 2016.

  1. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    I have a panel with a label and a button; to show and hide it, I use a canvas group.

    The problem is related to the click: since I use multiple panels that share the same screen space, they may end up on top of each other, and if the panel is higher in the hierarchy, it will block clicks on the other panel that is actually displayed on top.

    I did disable "block raycast" but this disable everything that is a child of the panel, including the button.

    As now, the only solution that I found are either to manually set the hierarchy so the topmost panel is always at the bottom of the hierarchy (so for Unity UI, it is the topmost item), or to use "interactable" and set it to false, when I hide a panel.

    Is there something else that I didn't consider, beside the solutions that I did mention?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    I'm confused. Are you using the canvas group because you want to use the alpha to hide it?
    If you're just using gameobject.setActive(false) on a parent, all the children deactivate as well.
     
  3. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    Yes, the panel use the alpha of the canvas group, to show and hide controls and UI elements via code.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Ok, but what is the reason you're doing it this way instead of disabling it?
     
  5. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    Because when I designed the code for the UI, I was under the impression that when you disable a component you can't enable it again via code, and since I need panels to appear and disappear, based on specific events, I simply made them invisible, without considering the implication that the panel was still enabled and catching mouse clicks.

    Now I am refactoring the UI code, so I am looking for a better solution, and I came up with the 2 that I did mention in the original post. Before going for either of the solution, I did stop by to ask if I did miss anything.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    If you have a script that is outside the object you're disabling, you can just use setactive(true)/setactive(false) to turn the panel on or off as needed. Then if you want the panel up, but the button not clickable, you can just set it interactable = false. Generally this is how I do panels or ui boxes.

    But that is why I asked because I didn't know if you were doing the alpha for a special reason.
     
    rapidrunner likes this.
  7. godspowerjemifor

    godspowerjemifor

    Joined:
    Jun 19, 2019
    Posts:
    3
    I have the same problem. How can I make the resume button to resume the game it seems not be working
     

    Attached Files: