How to properly hide a compound object

I have a compound object which consists of lot of GameObjects and i want to hide it with all children. I want to do it within script attached to this object.

By searching this forum i didn’t find a right way to do it. SetActive works well but i can’t access object after it was disabled. So this way is wrong. I can’t write a correct code for renderer, it says: Property or indexer `UnityEngine.GameObject.renderer’ cannot be assigned to (it is read only) or just hide a parent object, not children.

Can somebody post a code snippet to hide objects?

UPD: Probably the issue was in that i used foreach, which didn’t work.

2 Answers

2

You are looking for Renderer.enabled. There is probably a better or more elegant solution, but:

Renderer[] arrend = GetComponentsInChildren<Renderer>();
for (int i = 0; i < arrend.Length; i++)
    arrend*.enabled = false;*

Thanks, i tried something like this (with foreach) but it didn't work. Your code works!

I don't use foreach much, but this should do it: Renderer[] arrend = go.GetComponentsInChildren(); foreach (Renderer rend in arrend) { rend.enabled = false; }

transform.Find(“ObjectName”).renderer.enabled = false;

That is how you can hide the object using the renderer.
To hide all of the objects make a list of all the children gameobjects and use a loop to hide them all.

Hope this helps. :slight_smile:

Hmm, looks like robertbu gave you a better answer while I was writing mine. (tho haven't triad it)

All the same, thank you, you pointed me the way which i may use in other code.