Accessing Tree Nodes through Script


I was looking to ‘grow’ a tree. I have created a tree game object which consists of 6 nodes, 4 branch and 2 leaf. In Inspector → Hierarchy, the nodes are listed and I have the option of making the nodes visible (using the eye, show/hide).

Is it possible to create a script which will access the visible property of each node of the tree and assign a value? Allowing me to make each node visible, in turn, from root to leaf?

I have found plenty of information on transform, rotate etc but none on accessing the property of nodes.



You can loop through the children of a transform, and therefore through your hierarchy:

for (var treeNode : Transform in treeObject.transform) {
    for (var branch : Transform in treeNode) {
        for (var leaf : Transform in branch) {
            // use only one of the following lines
  ; // method 1: disables just the game object
            leaf.gameObject.SetActiveRecursively(false); // method 2 : disables the whole hierachy (if leaf has children)
            leaf.renderer.enabled=false; // method 3: just disables the renderer, not the object, making it invisible (assuming the leaf has a renderer attached)