(C#) How do I Enable/Disable Visualibility of an GameObject and its Children, Grandchildren, Greatgrandchildren and so on?
I have multiple GameObjects: GameObject1 GameObject2 GameObject3
These GameObjects have individual amounts of Children:
▽GameObject1
▽Child1
▽GrandChild1
▽Child2
▽GrandChild2
▽GreatGrandChild2
▽GameObject2
▽Child1
▽GrandChild1
Child2
....and so on ......
First Question: what is the most efficient way to enable Visualibility (Renderer, Layers...)? Second Question: How do I disable GameObject1 and GameObject3 with all there Children, GrandChildren... in C#?
It would be awesome if someone could help me out! Greetings From Germany!
Answer by Baste · Nov 25, 2015 at 12:59 PM
Being active is a recursive property, so if you set GameObject1 inactive, Child1, GrandChild1 and so on will also be inactive. So if you can just deactivate all of the objects, you can do:
GameObject1.SetActive(false);
If you want the objects to still be active (you have MonoBehaviours on them, or other components that still need to update even if the renderers are disabled), you'll have to find the renderers and set them disabled manually:
//GetComponentsInChildren also gets components on the object itself
Renderer[] allRenderers = GameObject1.GetComponentInChildren<Renderer>();
foreach(rend in allRenderers){
rend.enabled = false;
}
Good luck!