How to toggle between show and hide a GameObject?

Hi again guys,

I’m attempting to place a Game Object when something is triggered and would also like for it to be removed (or hidden) when I click on Close. How should i attempt to do this?

I’ve already tried with gameobject.SetActive(true) and gameobject.SetActive(false) but it doesn’t seem to take into consideration the “false” part of the statement.

Also I’ve tried using the Destroy(gameobject.clone) method but in no avail. Destroy(gameobject) destroys the object entirely and I’d like to be able to reuse it.

I read on the forums about using renderer.enabled = true and renderer.enabled = false. I’ve tried attaching a separate script for my gameobject on my camera and adding a mesh renderer to the camera but still it either showed the gameobject constantly or it didn’t at all.

Could you please enlighten me? I’ve become stuck on this for the moment…

You are setting the “master” as inactive, not the spawned one. You need to maintain a reference to the spawned ones and set those to inactive if you want to hide them. Alternatively, you could make the spawned monkeys child objects of the monkey3D. Although that might not work if the monkey3D is a prefab and not an object in the scene.

Method 1:

a) Make sure your script is using Generic Collections

`

 import System.Collections.Generic;

`

b) Add a new var.

`

 var monkeyList : List.<GameObject>;

`

c) Where you instantiate the monkey, do:

`

 var go : GameObject = GameObject.Instantiate( monkey3D, pos, rot );

 monkeyList.Add( go );

`

d) When you want to hide the monkeys (monkies?, monki??), do this:

`

 for (var i : int = 0; i < monkeyList.Count; i++)
 {
      monkeyList*.SetActive(false);*

}
* *Method 2:* *When you instantiate your monkey, do this instead:* *
var go : GameObject = GameObject.Instantiate( monkey3D, pos, rot );
go.transform.parent = monkey3D.transform;
`
-
Honestly though, It would be better if you used method 1 rather than method 2. It may have more code, but it is a much better way of doing it. If you don’t want to use a list, you can always use an array instead… I just find lists much easier to use.