- Home /
Can child game object with OnTriggerEnter function use parent game object's collider?
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.
Answer by icehex · Apr 28, 2020 at 01:25 AM
You could create a function in the child's script that is called by the parent's script when the parent observes OnTriggerEnter2D. The parent would need to be enabled as isTrigger.
 // This is the script attached to the parent
 public class ParentTrigger: MonoBehaviour
 {
     private void OnTriggerEnter2D(Collider2D col)
     {
         Debug.Log("A collision has occured in parent " + col);
 
         ParentTriggerDetect child_script = this.GetComponentInChildren<ParentTriggerDetect>();
         child_script .ParentTriggered(col);
     }
 }
 // This is the script attached to the empty child GameObject
 public class ParentTriggerDetect : MonoBehaviour
 {
     public void ParentTriggered(Collider2D col)
     {
         Debug.Log("Child has observed the parent's triggering" + col);
     }
 }
Thanks for the answer. That's what I ended up using. Hmm... I wanted to use various Unity physics functions (such as OnTriggerEnter, Stay, Exit, etc...) on the child object, but for now I think the parent object script relaying those trigger events onto child object's script is the workaround for my issue.
If anyone else know there's a way for the child object to use OnTriggerEnter using parent's collider, please let us know.
Answer by yummy81 · Apr 28, 2020 at 06:17 AM
You can use the observer pattern. For example with delegates. 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ParentScript : MonoBehaviour
 {
     public event System.Action action;
     
     void OnTriggerEnter2D(Collider2D collider) => action?.Invoke();
 }
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ChildScript : MonoBehaviour
 {
     ParentScript parentScript;
 
     void Awake() => parentScript = transform.parent.GetComponent<ParentScript>();
     
     void OnEnable() => parentScript.action += Foo;
 
     void OnDisable() => parentScript.action -= Foo;
 
     void Foo() => print(this);
 }
 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                