- Home /
Get the collider component of a child element
I've been looking at this for a while now but haven't managed to get it working.
I am using the Photon Networking Engine to build an online game, at the moment when a user starts the game, it joins either the available room or creates a new one. This then instantiates a new instance of my character prefab. The character has a third person camera control script which initially used the characters capsule collider as a point to follow and zoom to. I have removed the capsule collider and added colliders to each child of the object to create a more accurate collider.
Now what I need to do is get the collider of the "Head" child of my character and set it as the target in the camera control script, also later I will introduce a 1st person camera control and will need to keep the position of the camera inside the "Head" child's position. How would I go about getting the collider component of my character object's child.
For reference the Head child is at vincent (the character object) > Hips > Spine > Spine1 > Spine2 > Neck > Neck1 > Head. I don't know if this makes a difference to the way it is done. Could I just give my Head child a tag or layer and get the collider component that way?
Thanks in advance.
Answer by Graham-Dunnett · May 27, 2013 at 01:34 PM
The hierarchy in Unity is through Transform components. All GameObjects have Transforms. So, see:
http://answers.unity3d.com/questions/10417/how-can-i-access-the-children-of-a-transform.html
to figure out how to traverse your hierarchy, then use GetComponent.
That looks perfect thanks. I assume I iterator through every child, then check to see if it is the "Head" child, if so use 'GetComponent();'. Would this be found using child.name?