Understanding Hierarchy with Script
Notes:
Understanding Unity Hierarchy with C# Script:
Creating trees for A and G objects:
A
--B
--C
----D
----E
----F
G
--H
----I
----J
--K
----L
Example Code:
using UnityEngine.SceneManagement;
using UnityEngine;
public class HierarchyProof: MonoBehaviour {
void Start () {
GameObject[] rootGameObjects=SceneManager.GetActiveScene().GetRootGameObjects();
//Debug.Log (rootGameObjects.Length);
//Debug.Log(rootGameObjects[0].transform.name);
//Debug.Log(rootGameObjects[1].transform.name);
//Debug.Log(rootGameObjects[2].transform.name);
//Debug.Log(rootGameObjects[3].transform.name);
//Debug.Log(rootGameObjects[0].transform.parent);
//Debug.Log(rootGameObjects[1].transform.parent);
//Debug.Log(rootGameObjects[2].transform.parent);
//Debug.Log(rootGameObjects[3].transform.parent);
//Debug.Log(rootGameObjects[0].transform.childCount);
Debug.Log(rootGameObjects[0].transform.GetChild(1).transform.parent);
}
}
}