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.

2 Answers

2

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

as of Unity 4.5 there is a GetComponentInParent method