How do you add a child through code
Ive looked through the documentation and havnt seen anything obvious.
So just as the title says, how might you create a parent/child through code?
Answer by Molix · Jul 18, 2011 at 05:15 PM
Assign the child's parent transform, e.g.
void Parent( GameObject parentOb, GameObject childOb )
{
childOb.transform.parent = parentOb.transform;
}
These days as a general rule use SetParent
- google
I'm having a problem where this is working, but I think that the scale of the child object is causing it to be added far away from the parent.
Check the childOb.transform.localPosition and childOb.transform.localScale. When you attach a child to a parent, the child object will not move in worldspace; so if you have it at 0,0,0, but the parent is far away from the origin, the child will still be far away from the parent. After attaching, just zero out childOb.transform.localPosition.