- Home /
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.
@Pramod659 your question is not very clear.
What do you mean by "hide"? and what user action would trigger the "hiding"?
Also, is this something you want to do in the editor only, or what?
@pako By "hide" I mean setting "render.enabled" to "false" for each gameobject but as per the order stated above. Initially, just one user action, say user hits "H" button and the whole assembly of gameobjects should disappear in the order as stated above.
@Pramod659 I understand that order of disabling is important, but it seems to me that unless there's some kind of delay between each renderer disabled, the user will not be able to notice the order, since they will all be disabled almost instantaneously. So, if you do think that a delay would be better to show the effect, what kind of delay would be good, 0.5s, 1s?
Also, since the sub assemblies are very big, I guess dragging and dropping the renderers in a public array or List in the Inspector is out of the question? However, if it's something you'd consider (done only once), then setting the order of disabling is just a matter of adding the renderers in the correct order in the array/List.
@pako Ya infact I need a delay of 1sec. It is not just one time activity for one assembly. I need it for different levels and corresponding assemblies of my project.
Dragging and dropping the renderers in a public array or List in the Inspector is out of the question? ---YES
@Pramod659 you will need to set the order somehow, it cannot be done automatically, since the order is not entirely sequential.
From what you say, it seems that hard coding any kind of array to set the order would be too laborious to implement. So, the best bet is to figure out an algorithm to do it.
Answer by pako · Jun 26, 2017 at 04:28 PM
From the example you posted the following seems like a solution:
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.
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: https://docs.unity3d.com/ScriptReference/Transform.GetChild.html
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: https://docs.unity3d.com/ScriptReference/Transform-childCount.html
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.
The process of creating the collections in #1 - #4 above is naturally in the general order that you want to disable the renderers.
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:
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.
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.
@pako Thanks for your valuable suggestion. I shall try and assemble pieces of code to make the logic work which you have stated above and hopefully get it working
Your answer
![](https://koobas.hobune.stream/wayback/20220612124852im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Accessing a GameObject from an EditorWindow 3 Answers
Access a instantiated gameobject from a enemy spawner 4 Answers
SOLVED - Accessing a C# script value from another GameObject's JavaScript 2 Answers
Using Empty GameObjects to organize hierarchy? 2 Answers
Best workflow for DontDestroyOnLoad method regarding Unity Editor? 1 Answer