I am making a 2d tower defense game and need to detect clicks on towers and the game map. They both have colliders. Is there any way so that when a tower is placed and its collider overlaps with the map collider, only the tower’s OnMouseDown event triggers and not the map’s. (only click the “top” layer, not just turn off the map’s collider when mousing over another collider) I know that I could detect if the mouse is over the tower’s collider and use different logic from there, but I figured there should be a way to only detect a click event for one object when clicking on multiple colliders.
I know that I could detect if the
mouse is over the tower’s collider and
use different logic from there, but I
figured there should be a way to only
detect a click event for one object
when clicking on multiple colliders
You HAVE to change to a different logic. OnMouseDown just listens to the mouse, it has absolutely nothing to do with which collider you clicked on. You’re better off using just one OnMouseDown on a seperate GameObject and handle logic via raycast (Physics2D). You’ll probably add more to the scene that needs precedence over other things in which case you’ll be glad to have a “central management”.
Change Z-axis of map to 1, just further to camera than tower