Yakuman
Yakuman

Reputation: 183

Unity OnMouseUp() triggered when object is covered bu UI element

I have got some 2D elements in my scene with CircleCollider2D on them and a script with OnMouseUp(). At some stage a UI panel is rendered over the objects, and when I press a button that happens to be above the element with the OnMouseUp() it triggers both. I want to avoid disabling the circle collider every time the panel shows up and then enabling it after because of some other functionality. What can I do? Do I have to use raycasting?

Thanks for all help in advance.

Upvotes: 0

Views: 509

Answers (2)

Xa4
Xa4

Reputation: 73

You can check if the cursor is over UI. You can do this with the EventSystem.IsPointerOverGameObject() method, which returns a boolean. The documentation for the method.

Upvotes: 0

Nefisto
Nefisto

Reputation: 609

The OnMouseButton() does not answer objects in the Ignore raycast layer, so change the layer between those will solve your problem, another approach is to make an event when you open and close your menu and a bool field in your character like IsClickable, then you just toggle it as the answer to UI open/close event. Hope this helps

Upvotes: 1

Related Questions