- Home /
Change gameObjects parent at runtime
Hi all,
I have a script which at runtime moves a gameobject from one parent to another (from A to B).
  myobject.parent = ParentObject.transform;
And it works, but myobject has 2 child objects, which decouple after the move, so myobject and it's children all become siblings, the hierarchy is not preserved. How can i circumvent this? [so that when myobject changes parents, the children attached to myobject remain it's children]
Illustration of what is happening if i wasn't clear above.
 Pre move:
 -ParentA
 --myobject
 ---myobject_child1
 ---myobject_child2
 
 Post move:
 -ParentB
 --myobject
 --myobject_child1
 --myobject_child2
Answer by Garazbolg · Jul 18, 2017 at 02:11 PM
Why do you iterate through the children ? If you want your Hierarchy to stay you only need to change the parent of "myobject". Its children will follow.
So if i have :
 -ParentA
  --myobject
  ---myobject_child1
  ---myobject_child2
And I want
 -ParentB
  --myobject
  ---myobject_child1
  ---myobject_child2
I just do : myobject.transform.SetParent(ParentB)
But if i want to switch all ParentA children to ParentB and keep the hierachy under their children (Which I think is what you are trying to do) I do :
 void TransferAllA2B(Transform a, Transform b){
         bool WorldPositionStayTheSame = false;
 
         Transform kid;
         while(kid = a.GetChild(0)){
             kid.SetParent(b,WorldPositionStayTheSame);
         }
     }
Now that i think about it, you error was to think that 'GetComponentsInChildren()' apply only to the children directly underneath, but no it goes recursively, and even worst : the first array element is the object on which you called the function.
Ooooh, i didin't know that, what that explains a lot of trouble i've been having. I can't wait to get home and try this, will post results.
Thnks,
Tnx man, Tried this last night and it worked.
Had to refactor your code a bit since i use an Enumerator, but this was the key: kid = a.GetChild(0)
Answer by Cornelis-de-Jager · Jul 13, 2017 at 09:59 PM
Only change the parent of the one object not its children. So don't add / apply the script to the children objects.
I am not, or at least i think i'm not. The script which is doing this is attached to Parent B, which has a gameObjcect reference to Parent A. I then iterate through all the children (not grandchildren) found in Parent A and change the parent of each child, i assumed that the children attached to myobject in Parent A wouldn't decouple after the switch. 
How are you looping through your children? I cannot reproduce your issue when I use my implementation of iterating through the children.
 private IEnumerator SpawnCloud()
     {        
         while (true)
         {
             _Cloud = GetCloud(ParentA);
             TransferCloudToParentB(_Cloud);
 yield return new WaitForSeconds(SpawningSpeed);
         }
     }
 
 private Transform GetCloud(GameObject parentOBJ)
     {
         var children = parentOBJ
             .GetComponentsInChildren<Transform>()
             .Where(c => c.gameObject.GetInstanceID() != PoolA.GetInstanceID())
             .ToList();
         return children[children.Count - 1];
     }
 
 private void TransferCloudToParentB(Transform cloud)
 {
 cloud.parent = ActiveClouds.transform;
 }
I added the relevant code and removed everything else, hope this helps.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                