Parent object to child of another?

How do I get my turret to be parented onto the specific child of another object IE a car or vehicle. More specifically a bone within a child of this vehicle. So child of a child.

I can’t get this code to work… Do I have to use GetChild or can I use FindChild?

Code so far.

public GameObject turret;
public GameObject body;



void Update(){

if(Input.GetKeyDown(KeyCode.H)){

turret.transform.parent = body.transform.FindChild("TurretBone");


}

}

No errors, but nothing happens as said before.

A – Really, do not use “FindChild” which is in the weird “quasi-undocumented” space that we all love about Unity!

B – Change it to simply Transform.Find(“TurretBone”) which is absolutely the same thing

Go down to Shawn’s post… http://forum.unity3d.com/threads/133733-Where-is-transform-FindChild()

C – It’s very likely it’s simply not finding that item. Note that you have a typo here: publci (line 2)

Triple-check that you have the serialized variables set in the editor, in the inspector, and so on.

Add one line of code Debug.Log to check if “TurretBone” is being found … read the doco Unity - Scripting API: Transform.Find

Also try just making “turretBone” a public variable, and drag it in the editor, and see if it works that way.

You’ll resolve your problem in seconds, cheers.