Click single sprite in a pile of sprites

60230-2raycastsample.png

I have problems getting the right sprite to be clicked if there are multiple sprites on top of each other.

Attached is a sample project with a scene that lets you test the problem:
Here is the methods I have tried:

using UnityEngine;
using UnityEngine.UI;


public class InputControll : MonoBehaviour
{
    public GameObject sockClickCorrect;
    public LayerMask socksLayer;
    public Text objectToDestroyLabel;



    // Update is called once per frame
    void Update( )
    {

   

        if ( Input.GetMouseButtonDown( 0 ) )
        {
         //   RaycastAllClickDetection();
         //   RaycastAllClickDetectionUseTheLastHit();
            RayCastClickDetection();

        }
    }

    void RayCastClickDetection( )
    {
        objectToDestroyLabel.text = "";
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 5f, socksLayer);
        if ( hit )
        {
            //if(hit.transform.)
            //  Debug.Log( "Hit " + hit.transform.name );
            objectToDestroyLabel.text = hit.transform.name;
            // Destroy( hit.transform.gameObject );
        }
    }

    /// <summary>
    /// This sems to work if there are only two overlaping objects
    /// </summary>
    void RaycastAllClickDetectionUseTheLastHit( )
    {
        objectToDestroyLabel.text = "";

        RaycastHit2D[] hit = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 5f, socksLayer);

        if ( hit.Length < 1 )
        {
            return;
        }

        if ( hit[ hit.Length - 1 ] )
        {
            //   Destroy( hit[ 0 ].transform.gameObject );
            objectToDestroyLabel.text = hit[ hit.Length - 1 ].transform.name;
            Debug.Log( "Destroy " + hit[ hit.Length - 1 ].transform.name );
        }

        for ( int i = 0; i < hit.Length; i++ )
        {
            Debug.Log( i + " " + hit[ i ].transform.name, hit[ i ].collider.gameObject );
        }

        Debug.Log( "Amount hit: " + hit.Length );

    }

    void RaycastAllClickDetection( )
    {
        objectToDestroyLabel.text = "";

        RaycastHit2D[] hit = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 5f, socksLayer);

        if ( hit.Length < 1 )
        {
            return;
        }

        if ( hit[ 0 ] )
        {
            //   Destroy( hit[ 0 ].transform.gameObject );
            objectToDestroyLabel.text = hit[ 0 ].transform.name;
            Debug.Log( "Destroy " + hit[ 0 ].transform.name );
        }

        for ( int i = 0; i < hit.Length; i++ )
        {
            Debug.Log( i + " " + hit[ i ].transform.name, hit[ i ].collider.gameObject );
        }

        Debug.Log( "Amount hit: " + hit.Length );

    }
}

You can use RayCastAll and Linq to order the elements of the returned array by distance, and then either access the first one directly or iterate through the array until you find the one you want.

using System.Linq;

	
		 RaycastHit2D[] hits = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 5f, socksLayer).OrderBy(h=>h.distance).ToArray();
		foreach(RaycastHit2D hit in hits) {
                         Debug.Log(hit.collider.gameObject.name);
                }