- Home /
The question is answered, right answer was accepted
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.
Answer by Fattie · Sep 03, 2013 at 12:54 PM
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 http://docs.unity3d.com/Documentation/ScriptReference/Transform.Find.html
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.
Cool man thanks, Its just getting late here, can't concentrate so easy lol, need to sleep.
Thanks again. Its sorted.
It was because I was using FindChild. Find alone worked.
great stuff, D$$anonymous$$
Just think of me as Buckethead of the forum :)