x


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;
}
more ▼

asked Jan 27 '10 at 11:42 PM

SpoonyBard gravatar image

SpoonyBard
36 2 3 4

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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

more ▼

answered Jan 28 '10 at 01:04 AM

Sebas gravatar image

Sebas
4k 12 18 45

@Sebas, i'm having the same issue with some children. but i want to enable them after a collision, sense I have them inactive themselves at the start, but I can't find the script to call the child. How would I enable or activate the child from the parent?

3 days ago EG-Hernandez
(comments are locked)
10|3000 characters needed characters left

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)

more ▼

answered Jan 28 '10 at 01:12 AM

Duke gravatar image

Duke
418 1 5 11

when you set gameObject.active = false, Update() no longer gets called every frame, therefore your code to set gameObject.active = true will never run.

WTF? That's so stupid and counter intuitive! I just wasted the last 6 hours wondering why! So now what? Can you reference an inactive transform from an active one? How can I reference an object WITHOUT "find" (searching through every object in the game)??? THANKS

Feb 16 '11 at 11:33 AM vished

you could make a variable for the object ex "var building : Transform;" then in script say building.active = false; or true; or how you had it befor but this script should be attached to something that will never be disabled.

Jul 10 '11 at 02:58 AM Ninjaoboy

How is that stupid and counterintuative? You turn it of - it no longer runs.. sounds like expected behavior to me.

There are tons of alternative to using find. You could even establish the reference before run-time in the inspector.

Jul 10 '11 at 03:06 AM Joshua
(comments are locked)
10|3000 characters needed characters left

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 :)

more ▼

answered Feb 12 '11 at 04:32 AM

alexmon27 gravatar image

alexmon27
28 3 3 7

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Feb 23 '10 at 09:45 AM

Khesan gravatar image

Khesan
34 1

.active is not deprecated for GameObjects. It's only deprecated for components, because you're supposed to use .enabled on them.

Feb 23 '10 at 11:01 AM Eric5h5

Precisely. As Eric5h5 says.

Nov 20 '12 at 02:54 AM create3dgames
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Mar 14 at 01:31 AM

chetanisinanand gravatar image

chetanisinanand
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x2091
x718

asked: Jan 27 '10 at 11:42 PM

Seen: 39082 times

Last Updated: 3 days ago