- Home /
Clicks are detected through the UI elements
I have a scene with some 2D objects with colliders that detect clicks (OnMouseUpAsButton). I just have added a button and it seems to work (it changes its color when clicked, etc), but when the button is over one of the aforementionend 2D colliders, the click is detected by both elements.
How can this be avoided? That is, I want the click to be detected only by the element which is over; namely, the button.
I have added it by the Unity menu and a 'canvas' and an 'eventSystem' were added, so I think that I am using 4.6 (I can't check it right now)
Answer by AndresBarrera · Jan 13, 2015 at 08:23 AM
I have not tested this yet, but maybe you could use the IsPointerOverGameObject function. I think your 2D colliders will work with an input system different from the UI EventSystem, and before you take actions on them you can check if the EventSystem is registering input on some UI element.
After your answer I have the feeling that combining 2d colliders that receive mouse events with 4.6 UI objects it is not the usual way to work.
I'll read the documentation about the new EventSystem to learn the beaten path.
I have finally done what you said to me. I have tried other possibilities and yours seems the simplest. And it works :)
Answer by Juice-Tin · Jan 13, 2015 at 07:54 AM
For a sort of quick fix you can make a global variable.
class Menu
public static bool GuiClick;
void OnGui(){
GuiClick = false;
if(button) GuiClick = true;
}
//Elsewhere...
void Update(){
if(Menu.GuiClick) return;
//Game stuff
}
This isn't tested, as the Gui will run at a different speed than Update, but it's worth a shot.
Your answer
