The parent object has Rigidbody2D component, CircleCollider2D component, and a script to project it forward on each update.
The child object attached to the parent object is an empty game object with a script, which has a OnTriggerEnter2D function in it.
public void OnTriggerEnter2D (Collider2D collision)
{
if (collision.tag == "Enemy")
{
Debug.Log("Let's do this!");
}
}
Obviously, when the parent object collides with (or triggers with) the game object with the tag “Enemy”, nothing will be called. I assume that’s because child Object searches for the rigidbody and collider components in its own game object, and find nothings.
…
I tried to assign the collider by:
public Rigidbody2D modifierRigidbody;
public Collider2D modifierCollider;
public void Awake()
{
modifierRigidbody = GetComponentInParent<Rigidbody2D>();
modifierCollider = GetComponentInParent<Collider2D>();
}
Which did not work.
So, the question would be, is there any way you can let the child game object with a script to use / borrow the collider specified in the parent object?
Due to the design of the game, this parent-child game object relationship cannot change. It is necessary for the child object’s script to have OnTriggerEnter2D function, not on the parent’s game object’s script.