- Home /
Get component in parent
Hello everyone,
I've got a really simple question, that I wasn't able to solve though: Is there a way to get a script component from an object's parent?
I'll explain better the situation. Each enemy in my game has a trigger collider box as a children with the tag Damage, if the character's sword colliders with that trigger box, the enemy Dies, so when the sword touches the trigger collider tagged damage, I want to access the Die() method from his parent, the enemy, how can I solve this?
Thanks for asking this. It was annoying because I was trying to get the component from the game object and it wasn't working. I didn't know you could do it this way.
Answer by DaveA · Aug 20, 2011 at 04:30 PM
transform.parent.GetComponent(........
Oh.. sorry, I didn't know it was that easy, I should have searched better in the documentation before asking. Thank you
also, as of 4.5 there is a GetComponentInParent method
Answer by shadesbelow · Sep 26, 2014 at 10:13 PM
as of Unity 4.5 there is a GetComponentInParent method
Your answer
![](https://koobas.hobune.stream/wayback/20220612184952im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Find all gameObjects with same tag 1 Answer
Object Reference Required To Access A Non-Static Field 1 Answer
Getting Variables From An Arbitrary Component? 0 Answers
Find Script in parent Gameobject? 3 Answers
How to clone or re-parent a Component? 4 Answers