- Home /
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.
Answer by robertbu · Jan 17, 2013 at 05:08 PM
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[i].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;
}
Answer by dorpeleg · Jan 17, 2013 at 05:11 PM
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. :)
Hmm, looks like robertbu gave you a better answer while I was writing $$anonymous$$e. (tho haven't triad it)
All the same, thank you, you pointed me the way which i may use in other code.
Your answer
Follow this Question
Related Questions
How can I show/hide all models in a GameObject? 1 Answer
how to use itween with the object attached to other moving object 0 Answers
Turning off renderer in other game object (C#) 2 Answers
Renderer on object disabled after level reload 1 Answer
Changing material of objects by the tag in different scenes 0 Answers