Hi! I have a child object that is rendered on top of its parent object. They both have trigger colliders.
I’m trying to use raycast to detect when I click the child object, but the RaycastHit2D returned has the collider of the parent object and not the child.
private RaycastHit2D hit;
void Update()
{
if (Input.GetMouseButtonUp(0))
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
hit = Physics2D.Raycast (worldPoint, Vector2.zero);
Debug.Log(hit.collider + " using raycast");
//Debug.Log(hit.transform.gameObject.name + " using raycast");
//Debug.Log(hit.collider.transform.name + " using raycast");
//Debug.Log(hit.collider.name + " using raycast");
// All commented outs above give the same result as the one not commented out
}
}
However, using the following method in a script on the child object correctly returns the child object.
void OnMouseUpAsButton()
{
Debug.Log (gameObject.name + " from OnMouseUpAsButton");
}
So to be clear, when I run the game and click the child object, the Debug.Logs print:
“childObject from OnMouseUpAsButton”
“parentObject using raycast”