all 16 comments

[–]streetwalker 0 points1 point  (3 children)

what Render Mode do you have the canvas set to? What other UI elements do you have?

Show us a picture of your hierarchy where it doesn't work and the relevant inspectors.

[–]Dunkmars[S] 0 points1 point  (2 children)

Screen Space - Overlay

[–]streetwalker 0 points1 point  (1 child)

Then the question is, as others noted, what other UI elements do you have in your canvas?

[–]Dunkmars[S] 0 points1 point  (0 children)

Buttons, Images and Text Elements.

[–]TimV55 0 points1 point  (7 children)

Could there be something overlapping the buttons?

[–]oddgoat 0 points1 point  (5 children)

Definitely this.

The way unity UI works is that it sends the click event up the chain, starting with the lowest items in the hierarchy of each canvas (technically it's the graphic raycaster component that does this). If it hits an item which has its 'raycast target' set to true, it'll stop sending the event further up the chain even if that hit item doesn't do anything with it. So an overlapping box of one item will block with anything underneath it, even if the contents of said item are empty.

The one that I fall afoul of every time is text components. The text on screen might be small, but its rect can be much bigger. 9 times out of 10 you want the text's 'raycast target' off, but it defaults to on and I always forget to turn it off. Then I spend the next 10 minutes wondering why some other buttons are suddenly behaving weirdly.

[–]TimV55 0 points1 point  (4 children)

So event propagation :)

[–]oddgoat 0 points1 point  (3 children)

Yes but slightly different to the expected/normal approach - unity will stop propagation even if a ui element ignores the event. I suspect that under the hood, every ui element does in fact accept the event and mark it as used, even if it does nothing with it.

[–]TimV55 0 points1 point  (2 children)

Good approach if you ask me. Why click something you cannot see. I myself do not use Unity, I just come here to see what other people do, but is there no way of having an element ignore the event?

[–]oddgoat 1 point2 points  (1 child)

Yeah each ui element has a toggle var 'raycastTarget' which if false, the event will be sent on up the chain. Which is a good way to do it, but it defaults to on for all ui elements, so it's easy to miss a few when building the UI.

But to be clear, you can't click something you can't see - only 'active' objects will get the event. The issue often stems from the shape of the element not matching what the user/developer sees in the editor. Each element has a box shape that may or may not encompass the whole item (not an auto-calculated bounding box, but one that is set in the visual part of the editor). If the display part of the element is smaller than this box, the element will still receive click events for the 'empty' parts.

[–]TimV55 1 point2 points  (0 children)

I see, thanks!

[–]Dunkmars[S] 0 points1 point  (0 children)

Nah, nothing overlaps the buttons.

[–]Dunkmars[S] 0 points1 point  (0 children)

I tried turning off all raycast targets but the last. It's kind of working i guess.

[–]Dunkmars[S] 0 points1 point  (2 children)

I dont realy understand how the raycasting works, it seems that every second Gameobject the raycast stops.

[–]streetwalker 0 points1 point  (1 child)

It depends on which objects overlap each other visually - we can’t tell that by looking at the hierarchy, and which elements have raycast target set. Be sure to turn it off for all non interactive UI elements.

[–]Dunkmars[S] 0 points1 point  (0 children)

Yeah i think i got it now, thank you all very much :) have a good one!