- Home /
OnMouseDown gets both Collider and UI Button
Hi Everyone, I'm making a 2d game, and I'm implementing some buttons on the game screen.
These are the elements on the screen:
Game Objects (let's call it "Soldier") in the level, with a sprite rendere and a box2d collider. I'm using a OnMouseDown event to select units on the screen.
UI Buttons, that are
The situation I'd like to solve is the following: If I click on the button and there's a "Soldier" under it I also select the Soldier.
I'd like to avoid that. If I click on the button I'd just like to use the button and do not select anything below it.
Use a layer and raycast to an element of that layer (in your case UI).
When I click the mouse I can cast a ray and make it stop if it hits the button, but I'd like to try using the On$$anonymous$$ouseDown event. Is it impossible?
Do you also want to raycast on the Soldier? If not, then switch their layer to "Ignore Raycast". On$$anonymous$$ouseDown works on both UI elements and colliders, this is why you're having the problem.
I understand the issue of On$$anonymous$$ouseDown. I'd like to know if there's a simple solution, or also a complex one. Otherwise I'll create a raycast and make it stop if it reaches the UI button.
Yes. I want also to "raycast" the Soldier.
If there's only the solider I'd like to select it (using On$$anonymous$$ouseDown). If I've both button and soldier I'd like to just select the button (using the Unity UI).