Gameobjet rendering control

Hi,

I’m trying to do something very simple but I’m quite new to Unity so it’s hard to find the good way to do that…
Basically I have one GameObject which does contain some other GameObjects
And each of these GameObjects is actually a cube
I’d like to be able to control per script which GameObject must be displayed (or visible)

in pseudo script it would look like this :

if (displayGameObject1)
  gameObject1.setVisible(true)
  gameObject2.setVisible(false)
else if (displayGameObject2)
  gameObject1.setVisible(false)
  gameObject2.setVisible(true)

any idea ?
thanks in advance !
oX

You can store a reference to each child object and then turn it’s renderer on and off.

To find a child, you can use
cube1 = transform.Find(“name of object”);
cube2 = transform.Find(“2nd name of object”);
etc.

You will have to do this call for every child object you want to control. Be sure they all have different names, since I believe the function returns the first one it finds of that name. Therefore, if they are all the same name, you will get the reference to the first child found on every call. Do these calls in either the Awake or Start functions.

To toggle a renderer off:
cube1.renderer.enabled = false; //or true to turn it on

Note that this only turns visibility off and on. The children objects will still do Update calls if applicable.