Perform Action only if click not above UI-Element

Hey Community,

How can i check if a click (or a touch) is above an UI-Element and only do something if it’s not?
I thought this would be easy but after I tried Raycasting with the UI-Layer blocked and Canvas Groups I still didn’t find a solution.
My Problem is that an action has to be performed unless the player is clicking e.g. a button, and currently I dont have any idea on how to do that (also there isnt much information about that and the new UI system on the internet yet so I have to ask such a simple question).

Thanks,
Claas M.

You might try EventSystem.IsPointerOverGameObject(): Unity - Scripting API: EventSystems.EventSystem.IsPointerOverGameObject

1 Like

Still returns true when the pointer is over a UI Element

Do you want to do something only if the mouse is anywhere but one specific UI element, or do you want to do something if it is not over any UI element? The question can be understood both ways.

1 Like

ClaasM, I was told EventSystem.IsPointerOverGameObject should really have been called EventSystem.IsPointerOverUIObject

In the documentation it says “Is the pointer with the given id over an EventSystem object?” In your case you want to use

if (EventSystem.IsPointerOverGameObject =! true)

Please be aware that if you have a panel that is stretched over the entire screen of your device it will always return true.

iByte

1 Like

Thanks iByte, that solved it!
(Also i meant over any UI-Object)

Claas M

1 Like