Hello Unity Forum!
The OnMouseDown Callback (and similar mouse events) can not only be used by UI-Elements, but also by game objects with a collider. However, if multiple colliders overlap with the mouse position when the mouse button is pressed, only one game object gets OnMouseDown callbacks called.
In a 2D Game Setting, Colliders/GameObjects are considered by Z-axis order and when it comes to a draw, it is seemingly random which object gets the call. (Creation/Edit order of the game objects in the editor seems to have something to do with it)
However, in my opinion, a very big use case that a lot of developers will intuitively go for with OnMouseDown is giving game objects with sprite renderers a 2DCollider to make them clickable. In this specific scenario Z-axis order will always result in draws (z typically being 0). However, there already exists a very explicit order on the game objects: the rendering order.
My request/proposal is:
Extend the usage of GetRayIntersection that currently determines which game object receives the callback on mouse clicks, so that draws (that are currently resolved randomly anyway) favor game objects with sprite renderers, with them being ordered like with rendering (layer + order in layer). Basically, in a 2D Game a Mouse Click should hit the frontmost sprite, since users typically click what they can see.
Thank you for your Time!