Enable/Disable objects, with script

Hi, I was wondering if anyone could give me some help:

I have a project, and will have two scenes - a "before" and an "after" world. The "after" world will have lots of different objects that I need to be able to turn on and off via script, because the user will be purchasing changes to the world.

Here's my quick test code, I thought it would work, but the item won't re-enable when I check the box in the inspector's script section.

var onoff : boolean;
function Update () {
    if (toggle == true)
    	gameObject.active = true;
    if (toggle == false)
    	gameObject.active = false;
}

GameObject.active is Obsolete in Unity 4,
Rather Use: gameObject.SetActive (false);
Or GameObject.activeSelf, GameObject.activeInHierarchy

I tried the following:

var onoff : boolean;
var testObject : GameObject;

function Update () {
    if (onoff == true)
        testObject.active = true;
    if (onoff == false)
        testObject.active = false;
}

The script works fine when attached to something like the First Person Controller. As testObject I used a sphere. When I attach the script to the same object which I want to disable, I got a small problem.. That is, if you disable the gameObject to which your script is attached, the script won't get called anymore and your re-enabling your onoff variable won't have any effect. Hope that makes sense.

One option I could think of is to parent your objects to an empty game object, attach the script there and simply disable the child. That should do the trick, but there might be more elegant solutions out there.

/edit

Another option would be suitable if you only want the objects to either be rendered or not rendered. You could enable/disable the renderer like this http://unity3d.com/support/documentation/ScriptReference/Renderer-enabled.html

I assume you mean the gameobject won't re-enable when you set the script's onoff variable to true in the editor, and that when you referenced "toggle" in your Update function, you really meant to reference onoff.

I think what you are running in to is that when you set gameObject.active = false, Update() no longer gets called every frame, therefore your code to set gameObject.active = true will never run.

If you have a reference to your script object to set the onoff variable (script.onoff = true), could you just access the gameObject directly from there? (script.gameObject.active = true)

The statement .active is now deprecated, use .SetActive(true/false) instead.

I had this same issue with a guiTexture since you cannot use the renderer property... my easy fix was to make my GUITexture object a child of an empty game object. So the hierarchy was gui_textureUpdated > gui_texture. I attached my script to the empty game object (gui_textureUpdated) that looked something like this...

var isTextureVisible : boolean;
var someTexture : GUITexture;

function Update()
{
    if(isTextureVisible)
        someTexture.gameObject.active = true;
    else
        someTexture.gameObject.active = false;
}

If you are using anything else that does use the renderer property, just use gameObject.enabled instead of .active. This should fix your visibility issues. Hope this helped :)

gameObject.SetActive(false);

Heres what I did for my pause menu:

#pragma strict

var showingMouse : boolean = false;
static var pause : boolean = false;
var menucomponet1 : Transform;
var menucomponet2 : Transform;
var menucomponet3 : Transform;
var menucomponet4 : Transform;
var centercross : Transform;
 
function Start()
{
	showingMouse = false;
	Screen.lockCursor = true;
	renderer.enabled = false;
	pause = false;
	Time.timeScale = 1;
	menucomponet1.gameObject.active = false;
    menucomponet2.gameObject.active = false;
    menucomponet3.gameObject.active = false;
    menucomponet4.gameObject.active = false;
    centercross.gameObject.active = true;
}
 
function Update()
{ 
    if(Input.GetKeyDown(KeyCode.Escape) && showingMouse == false)
    {
    	showingMouse = true;
    	Screen.showCursor = false;
    	Screen.lockCursor = false;
    	var pause = true;
    	Time.timeScale = 0;
    	menucomponet1.gameObject.active = false;
    	menucomponet2.gameObject.active = false;
    	menucomponet3.gameObject.active = false;
    	menucomponet4.gameObject.active = false;
    	centercross.gameObject.active = true;
    	
    	
	} else if(Input.GetKeyDown(KeyCode.Escape) && showingMouse == true)
    {
    	showingMouse = false;
    	Screen.showCursor = true;
    	Screen.lockCursor = true;
    	pause = false;
    	Time.timeScale = 1;
    	menucomponet1.gameObject.active = true;
    	menucomponet2.gameObject.active = true;
    	menucomponet3.gameObject.active = true;
    	menucomponet4.gameObject.active = true;
    	centercross.gameObject.active = false;
    	   		
    }

Attach this script to an empty gameObject, and drag the parts of your menu into the Transform boxes here:

I am using this for a FPS so the center cross box is for the Center of the screen crosshairs.

i used gameObject.SetActive(false) to do this.

say, if i have a pause panel and want to show it only when player pressed the pause button, i can pausePanelGameObject.SetActive(false) at Start() and pausePanelGameObject.SetActive(true) when pressed the button.

However, if you untick the gameobject in the inspector you will get error as it will not be loaded so you cannot use SetActive on it.

Remember that .active is deprecated now, and you need to use .enable instead.