This will return a 2D collider at the current mouse position. It works by calculating the 2D position of the mouse from the Orthographic camera. Then, doing a 2d raycast with a single point(zero length), but infinite depth(distance from camera). This works because Physics2D.Raycast() can return colliders that the ray begins inside.
There are three ways for you to solve your problem:
Use as script with an OnMouseDown() function attached to each selectable object. OnMouse* functions work with both 2D and 3D colliders.
Use a 3D collider on a child object. You cannot have a 2D and a 3D collider on the same object, but you can have an empty game object as a child of your sprite with its own collider. You’ll need to pick and size the collider as appropriate to your sprite. You can use the transform.parent of the child object if you need to get access to the sprite and/or its scripts. You would use the original Physics.Raycast().
Here is a hack I discovered yesterday. First convert your mouse position into a world coordinate using Camera.ScreenToWorldPoint(). Then use that position to do a Physics2D.Raycast() with a very short ray (like 0.05). It does matter what direction, but the distance needs to be short.
This works for me by attaching the script to the main camera (Orthographic).
It is based on Spinnernicholas solution.
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Vector2 origin = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.zero, 0f);
if (hit) {
print(hit.transform.gameObject.tag);
}
}
}