Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Changing all parents to another in editor script not working

Discussion in 'Scripting' started by arisdev82, Mar 7, 2021.

  1. arisdev82

    arisdev82

    Joined:
    Feb 27, 2019
    Posts:
    39
    Hi boys!

    I have created a very simple editor script to change all childs inside a gameobject to a common parent but I can't understand what is wrong in the code.

    I'm using Unity 2019.4.11f1

    My original gameObject structure is this one

    upload_2021-3-7_8-3-15.png

    My final result is always like this:

    upload_2021-3-7_8-3-43.png

    My desired results needs to be like this:

    upload_2021-3-7_8-4-26.png


    The code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class SpecialFunctions : MonoBehaviour
    7. {
    8.     /////////////////////////////////////////////////////////////////////////////////
    9.     /// <summary>
    10.     /// ChangeAllParents
    11.     /// </summary>
    12.     /////////////////////////////////////////////////////////////////////////////////
    13.     [MenuItem("New transform Tools/Change All Parents")]
    14.     public static void ChangeAllParents()
    15.     {
    16.         // find base go
    17.         GameObject baseGo = GameObject.Find("SpecialTest");
    18.  
    19.         // create temporal go
    20.         GameObject temporalGo = new GameObject("TEMPORAL_" + baseGo.name);
    21.  
    22.         Transform baseGoTransform = baseGo.transform;
    23.         Transform temporalGoTransform = temporalGo.transform;
    24.  
    25.         ChangeAllChildsParentsRecursivelly(baseGoTransform, temporalGoTransform);
    26.     }
    27.  
    28.     /////////////////////////////////////////////////////////////////////////////////
    29.     /// <summary>
    30.     /// ChangeAllChildsParentsRecursivelly
    31.     /// @param _transform
    32.     /// @param _temporalGoTransform
    33.     /// </summary>
    34.     /////////////////////////////////////////////////////////////////////////////////
    35.     private static void ChangeAllChildsParentsRecursivelly(Transform _baseTransform, Transform _temporalGoTransform)
    36.     {
    37.         if (_baseTransform)
    38.         {
    39.             _baseTransform.SetParent(_temporalGoTransform, true);
    40.  
    41.             Debug.Log("Separating: " + _baseTransform.name + " -> new parent name: " + _baseTransform.parent.name);
    42.         }
    43.         else
    44.         {
    45.             Debug.Log("ERROR -> _baseTransform is null: " + _baseTransform.name);
    46.         }
    47.  
    48.         Debug.Log(_baseTransform.name + " -> childCount: " + _baseTransform.childCount);
    49.  
    50.         for (int i = 0; i < _baseTransform.childCount; i++)
    51.         {
    52.             Transform child = _baseTransform.GetChild(i);
    53.  
    54.             Debug.Log("Continue with child called: " + child.name);
    55.  
    56.             SpecialFunctions.ChangeAllChildsParentsRecursivelly(child, _temporalGoTransform);
    57.         }
    58.     }
    59. }
    60.  

    Console output shows that the recursive function is not entering all childs, I can't understand why.

    upload_2021-3-7_8-8-19.png


    After hours I'm getting crazy, so any help will be very appreciated!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You're changing the number of children the transforms have while trying to iterate over the children!

    Much simpler method to do this:
    Code (CSharp):
    1. Transform[] allChildren = baseGoTransform.GetComponentsInChildren<Transform>();
    2.  
    3. foreach (Transform t in allChildren) {
    4.   t.SetParent(_temporalGoTransform);
    5. }
     
    BenniKo likes this.
  3. arisdev82

    arisdev82

    Joined:
    Feb 27, 2019
    Posts:
    39
    Thanks @PraetorBlue !!

    Anyway, i can't understand why is he original code not working even if i was changing the parent in the end). If is the parent which is changing, not the childs, why is the child count chaning?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    On line 50, you are doing a for loop over the children of an object. Inside the loop you change the parent of the children (in the recursive function call). So let's say there are two children. You start with i = 0 and get the 0th child. You change its parent. Now there is only one child left. i++ makes i = 1. There is only one child now so childCount returns 1 and i is 1. i is not less than 1, so the loop ends, leaving a child unvisited.