- Home /
Detection OnTriggerEnter on the Child in Parent script.
I want to trigger hit of OnTriggerEnter on child were detecting in parent script. So how can I do that with minimum (preferably none) script on child itself. Please if anyone can give code example c#, not link to similar question, I read all I could find and I don’t get it.
Answer by DavidZendle · Apr 25, 2015 at 12:17 AM
I don't believe it's possible to call a child's OnTriggerEnter directly in the parent itself; that's just not how Unity is set up to work. However, if this functionality is what you want then a quick workaround is possible, by having your main function in the parent, and calling up the hierarchy from the child to get to it:
In the parent:
public class ParentTrigger: MonoBehaviour {
public void PullTrigger(Collider c){
Debug.Log("Your code goes here");
}
}
In the child:
public class ChildTrigger: MonoBehaviour{
void OnTriggerEnter(Collider c){
gameObject.GetComponentInParent<ParentTrigger>().PullTrigger(c);
}
}
Hope this helps.
If found answer to be correct. Please click the Tick.
SO other can know that the answer is correct.
Answer by Marbelous · Jul 26, 2018 at 11:23 PM
Thanks for the interesting solution! You helped me out.
Answer by vuuk · Oct 19, 2020 at 08:29 PM
Parent rigidbody , child trigger collider. OnTriggerEnter in patent component. Except theres no wy to know which child trigger is entered.
Your answer
![](https://koobas.hobune.stream/wayback/20220613193919im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make a simple tree 1 Answer
Parenting On Trigger not Working? 3 Answers
OnTriggerEnter - check if collider is child 1 Answer
Update a parent owned Script from within a child script 0 Answers
How to Instantiate Prefab as Child of Player OnTriggerEnter 0 Answers