Raycast2D Two Objects at the same time

I am trying to execute a function only when two objects, one of type “touchArea” and other of type “Tiles” . For that purpose, I did this

  void Update()
    {
        

        if (Input.GetMouseButtonDown(0))
        {

            Vector3 mousePos = Input.mousePosition;
            mousePos.z = 10;

            Vector3 screenPos = GameCamera.ScreenToWorldPoint(mousePos);

            RaycastHit2D hit = Physics2D.Raycast(screenPos, Vector2.zero);

            if (hit)
            {
                if(hit.collider.tag=="touchArea" && hit.collider.tag == "tiles")
                {
                    print(hit.collider.name);
                }
               
            }
        }

It works fine when only looking for one type, but when looking for two that are overlapping, it doesnt work. One of them is a trigger type. Thanks in advance.

You could use a if-else statement. Try if that works.

I’m having the same problem. The solution is simple, just use Physics2D.RaycastAll instead of Physics2D.Raycast. It will return an array of hit objects.