Traversing the child objects of Transform in unity

 

1. Traverse all sub-transforms of Transform

 private void Start()
 {
      var Equipment = building.transform.FindChild("building/building/Equipment");
        foreach (Transform tran in Equipment)
        {
            Debug.LogError(tran);
        }
  }        

2. Recursively traverse all child GameObjects of the GameObject

 public void Awake()
    {    
       Recursive(gameObject);
    }

// Recursively traverse all sub GameObjects of the GameObject 
 private  static  void Recursive(GameObject parenGameObject)
    {
        foreach (Transform child in parenGameObject.transform)
        {

            Recursive(child.gameObject);
            Debug.LogError(child);
        }
    }    

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025266&siteId=291194637