- Home /
Question by
Tomer-Barkan · Dec 01, 2013 at 06:38 PM ·
gameobjecttransformparentparenting
Parenting from code doesn't really change the hierarchy
So I tried creating a gameobject from code, and then setting the transform.parent
to this.transform
, but in the hierarchy of the editor it doesn't appear as a child, and also when the parent gets destroyed, the child remains...
Any reason to this?
void Start () {
GameObject newobj = new GameObject("Testing");
newobj.transform.parent = transform.parent;
GameObject.Destroy(gameObject, 2);
}
After 2 seconds, the original object is destroyed, but "Testing" is still there.
Comment
Best Answer
Answer by Mortoc · Dec 01, 2013 at 06:42 PM
Bug in your code. You probably want this:
void Start () {
GameObject newobj = new GameObject("Testing");
newobj.transform.parent = transform; // this transform, not the parent
GameObject.Destroy(gameObject, 2);
}
Exactly. You want your new object to be parented to the current transform, not the current transform's parent.
Duh... I feel so dumb... And to think that I copied the same error from my original script to the testing script :(