I’m encountering an issue where UI buttons can still be clicked even when they are obscured by a mesh (e.g., a wall or box) with colliders. This behavior feels like a bug or perhaps something I’m missing in my implementation.
Here’s the problem:
I want a UI button to only be clickable if it is visible to the camera.
Currently, even if the button is behind a wall or hidden from view, clicking in that area triggers the button’s click event.
I’ve created a demo project to illustrate this behavior: Demo Project Link.
In the demo, you’ll notice that clicking the button works even when it’s obstructed by a box.
My Expected Behavior:
When I click on an area hidden by the box, the button should not register a click event. Otherwise, it’s unclear how to distinguish between valid clicks and those that should be ignored.
Attempts So Far:
I’ve tried using raycasts to check for visibility, but since the UI button doesn’t have colliders, I haven’t found a working solution.
Any advice on handling this scenario would be greatly appreciated! Thanks in advance for your help.
Thank you all for your helpful replies and suggestions.
I wrapped the UI button in a rigid body and a collision component with zero resistance. Then, when a click event is triggered, I use a raycast to check which rigid body is actually selected. This ensures that only visible buttons can be clicked, and clicks obstructed by meshes (like walls or boxes) are ignored.
Updated Demo:
For anyone encountering a similar issue in the future, I’ve updated the example project to include this logic: Demo Project Link
Thanks again for all the insights and support—it made a big difference!