Prevent click on UI to affect game objects in scene?

Hi

I find that when I have a UI element active over a scene that when I click on the UI element (Button, Text, etc) that the click action also passes through the Canvas and is triggered on the game objects in the scene. How can I prevent that?

Kind regards.
Kobus

This problem can be solved in few ways. The best way is to use this: Unity - Scripting API: EventSystems.EventSystem.IsPointerOverGameObject

It returns true when the pointer is over Unity UI object that has option to recive events. In your controller of that gameobject you need to just check that.

The other way is dirty: you can create colliders behind the UI gameobject, but that requires having canvas not set to Screen Space - Camera.

When I started with Unity3D UI I was preaty suprised that option from GraphicRaycaster component that says to block objects, doesnt work as I though on first thought.

1 Like
4 Likes

Thank you for the link. I managed to get it working following the video clip.

1 Like

Awesome! And your view earned me a fifth of a cent from google. Everyone is a winner. :slight_smile:

But seriously, glad I could help.

1 Like