How to hide every game object as per Hierarchy in active scene?

I have a structure as shown in the image attached.

My requirement is to hide the game objects one by one in the order given below

Child1 >> Child2 >> Sub assembly1 >> Sub root1 >> Child 3 >> Child4 >> Sub root 2.

The whole assembly is very big and has lot of such sub assemblies and hence I need to script this.
I tried using Transform.GetSiblingIndex and gameObject.GetComponentsInChildren.() in loop without any success. Since I am new to C# scripting. Any help in this regard is highly appreciable.

From the example you posted the following seems like a solution:

  1. Create a collection of all roots (they don’t have a parent) - I think it’s best to tag each root and use the FindGameObjectsWithTag command to create the collection. There are other ways, and it’s up to you to decide, as you have the whole picture. However, FindGameObjectsWithTag returns a collection of GameObjects, but you need an array of transforms. So, you need to iterate through the returned GameObject collection and access the transforms to add to your Roots collection.

  2. Create a collection with all children of a root (indeces 0, 1, 2, … n). These are the subroots. You can iterate through the collection of roots, and for each root, add all its children to a new
    subroot collection. This will result in a subroot collection for each root. The GetChild(int index) command can be used for this: Unity - Scripting API: Transform.GetChild

  3. Each subroot has either a subassembly as a child, and several grandchildren (the children of the subassembly (e.g. child 1, child 2) OR children with no children (e.g. no subassembly, child 3, child 4). You can iterate through each subroot and check if its first child (index 0) has any children, by using transform.childCount. If the child count is greater than 0 then it’s a subassembly, otherwise it’s directly a child with no other children: Unity - Scripting API: Transform.childCount

  4. While on #3, create a collection of subassemblies, and a collection of the children of each subassembly, and also a collection of the children directly under a subroot.

  5. The process of creating the collections in #1 - #4 above is naturally in the general order that you want to disable the renderers.

  6. It might be best to create the collections once and save them, or you might prefer to create the collections every time the game is started. As you are iterating through the collections, the logic for disabling the renderers is the following:

  7. When you find a subassembly, iterate through its children and disable their renderers, then disable the renderer of the subassembly, and then disable the renderer of the subroot.

  8. When you find children directly under a subroot, iterate through the subroot’s children and disable their renderers, and then disable the renderer of the subroot.

I hope this helps you.