- Home /
Set the parent of children of a parent,How to set the children of a parent
So I have a simpel question. I have a empty gameobject(let's call it cat), which has 7 chldren I don't know which. I have them inside of another empty gameobject(let's call this dog). I want to detach the children from the cat but keep them in the dog. When using the transform.detachChildren() the children are being detached but lose their place in the hierarchy(they are being placed outside of my dog). So is there any way to set the parent of children of a parent. Like I am changing there parent to dog? I don't know the chldren but do know the dog and cat, so I can't just set the parent of each child. Hope this clarifies my question sufficient. Thanks for the help!
Answer by highpockets · Apr 22, 2019 at 07:54 PM
Make a list of the child objects before you detach them:
List<Transform> childrenTransforms = new List<Transform>();
foreach (Transform child in transform)
{
childrenTransforms.Add(child);
}
transform.detachChildren();
foreach (Transform child in childrenTransforms)
{
child.parent = dog.transform;
}
Hopefully that helps, Cheers
Your answer
![](https://koobas.hobune.stream/wayback/20220612195936im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make a simple tree 1 Answer
Have child ignore parent's tag? 1 Answer
How make an object child of other object without changing the child position? 0 Answers
Script that mimics a child object behavior. 1 Answer
Getting the topmost parent 1 Answer