Problem activating objects....

Hello. When I activate an object through a script like this, how can I make sure that all of the object’s children are also activated (and vice versa for deactivating it)? Also, how can I make an exception to this (ex: an object has 5 children, and I want to activate 4 of them)? Thanks

if (Genre==1) {A.active=true;};

To activate/deactivate an object and all its children, use GameObject.SetActiveRecursively().

To activate/deactivate selectively, you’ll probably just have to do it manually. Note that you can iterate over all the (first-level) children of an object by iterating over its transform (see the docs on Transform for details) which might help. And, of course you can use any of the usual means of referencing an object for the purpose of activating/deactivating it, or of reactivating it or re-deactivating it after using SetActiveRecursively() to activate/deactivate a game object and its children.

Also posted to Unity Answers.

Thanks. This is part of my script that I am using to turn on the options in my game. I set the variable and the “A” object will not turn on.

static var Ship  = 7;
var A : GameObject;

function Update ()
{ 
	if (Ship==1) {A.SetActiveRecursively(true);};
}

Are you getting any compiler errors? The code looks ok to me (aside from what I suspect is an extraneous semicolon after the ‘if’ block).

If it’s not working, try debugging it. Use the debugger, or add some debug output so that you can determine:

  • If the Update() function is being called
  • If the conditional ‘if (Ship==1)’ is evaluating to true
  • If A.active is true after the call to SetActiveRecursively()
  • Etc.

Hmmm, it appears that the script is not being called. I am using a toolbar script in a menu scene to determine the options for my game(a bit of that script is below). That then affects the static variable in the other script(“_Options”), which then affects which objects are active. For some reason, the toolbar script wont affect the _Options script. The toolbar script works if it is affecting static variables (I tested it using my games score). Any ideas?

static var GenreInt = 0;
function OnGUI() {
	GenreInt = GUI.Toolbar (Rect (240, 200, 500, 50), GenreInt, 
		["Classic", "Defender", "Vertical","Twin Stick"]);}

function Update (){
if (Arcade.GenreInt ==0) {_Options.Genre =1;}}

Hm, not really (I’m not sure if there’s enough information there to say what the problem is).

I got it. I figured out how to affect the variables in the other script. I needed to have an in between script that used DontDestroyOnLoad. So it goes GUI Script>Mid Script>Options>Turn objects on/off. But, the objects still will not turn on/off :frowning:

Never mind :(, the variables are not being changed.

Neve rmind…again. I forgot to turn the object with the script on :slight_smile: