- Home /
Is there a way to access Child colliders trigger from parent
I have a script attached to a parent object with OnTriggerEnter2D that I want it to be called when any of the children colliders is triggered.
OnTriggerEnter2D is only called when an object collides with the parent,I have a script attached to a parent object with OnTriggerEnter2D that I want it to be called when any of the children colliders is triggered.
I was thinking of a way to do it without adding scripts to children, it would have been a lot easier.
Answer by Buckslice · Dec 04, 2017 at 11:03 PM
Add a script like this to each child collider
ParentScript script;
void Start(){
script= GetComponent<ParentScript>();
}
void OnTriggerEnter2D(Collider2D col){
if(script){
script.OnTriggerEnter2D(col);
}
}
Answer by OrbitalTech · Dec 04, 2017 at 10:30 PM
If you put an OnCollisionEnter(Collision col) in your child, and then put in that method a call for your OnTriggerEnter2D.
void OnCollisionEnter(Collision col)
{
gameObject.transform.parent.GetComponent<scriptName>().OnTriggerEnter2D();
}
Your answer
Follow this Question
Related Questions
Problem with 2D movment system C#. Keeps moving when no command is given 0 Answers
My player is not rotating upwards when it moves upwards. Any advice? 1 Answer
Shooter 2D - Problem with rotation of bullet to face direction (velocity) 2 Answers
When an object bounces between two bouncy objects it goes through them 1 Answer