Click object behind other object

Hello guys. I have a small problem.
I have an object A rolling over object B. When i press on B i want to delete object A. The script is working but the problem is that sometimes i cant click on object B because A is all over it. So bassically object A is in the way of object B. How can i click through A?

List<GameObject> currentCollisions = new List<GameObject>();

// Add the GameObject collided with in a list when entering
    void OnTriggerEnter(Collider col)
    {
        currentCollisions.Add(col.gameObject);
      
    }

// Remove the GameObject from the list on exit
    void OnTriggerExit(Collider col)
    {   
        currentCollisions.Remove(col.gameObject);
   }
    // When clicking on object B all object above it get destroyed
    void OnMouseDown()
     {
         if (gameObject.tag == "cubes")
             foreach (GameObject g in currentCollisions)
             {
                 Destroy(g);
             }

     }

Thank you.

Hey, instead of OnMouseDown try and Use Physics.Raycast or Physics.RaycastAll in a manager object that is not directly related to the snowballs (this script is not a component of the snowball object). To use raycasts you need a ray. The best way to get a ray in your situation is to use Camera.ScreenPointToRay. Here is a tutorial that explains the entire raycast processes:

PS: if you want to use the script on PC use Input.mousePosition and Input.GetMouseButtonDown

Now, a normal raycast will retrieve the first object that the ray meets on the predefined layer. To get all the objects (both the snowball and the object behind) you use RaycastAll as described in the documentation example

1 Like

You can also supply a layermask parameter in a Raycast… if object b and object a can be on different layers. A layermask allows you to ignore all objects on a layer you choose.

The easiest way to do that is to declare the layermask variable as public, then check/uncheck the layers you want in your raycast as needed.

1 Like

Yep. All done. Thank you guys ^^
Here is the script:

List<GameObject> collisionList = new List<GameObject>();

    void Update()
    {//Detecting the click on gameobject with tag "cube"
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
           
                if (hit.transform.gameObject.tag == "cubes")
                {
                    foreach (GameObject z in collisionList)
                    {
                        Destroy(z);
                    }
                }
                 }
    }

    // Add the GameObject collided with, in a list, when entering
    void OnTriggerEnter(Collider col)
    {
        collisionList.Add(col.gameObject);
    }

   
    // Remove the GameObject from the list on exit
    void OnTriggerExit(Collider col)
    { 
        collisionList.Remove(col.gameObject);
   }
1 Like

For anybody who’s looking at this for new answers, switching object A’s layer to “Ignore Raycast” should make it so that mouse clicks and finger taps register whatever is behind it instead. (this only works if you NEVER want to click on A for any reason)

Ty @OwenKuhn .

This is exactly what i was looking for.