Optimize Activate / Deactivate Gameobjects Array

Hello All,

Im trying to figure out how to activate and deactivate a bunch of prefabs in a certain room when entered, My thought was to use a trigger that when entered would Activate a bunch of gameobjects I had in an array.

I have many rooms in this scene and this way I could have the objects and scripts attached to those objects only be active right before I enter the room. This way all of the objects in all of the rooms aren’t always running and rotating etc…

is there a solution to doing this below is a trigger script but I get an error because active is not a member of GameObject :frowning: so know I cant think of a way to do this? right now I have a lot of objects using if distance < something but this is slowing down the game because I have a lot of prefabs in each room which makes up the level.

Heres the script anyone have any thoughts?

var activeObjects : GameObject[];

function Start()
{
	activeObjects.active = false;
}

function OnTriggerEnter(other : Collider) 
{
	if(other.gameObject.tag == "myrobot")
	{
		activeObjects.active = true;		
	}
}
function OnTriggerExit(other : Collider) 
{
	if(other.gameObject.tag == "myrobot")
	{
		activeObjects.active = false;
	}
}

http://unity3d.com/support/documentation/ScriptReference/GameObject-active.html
gameObject.active wont work because then its not a valid type but it does use the active
I also found out .enabled is not a member of Unity.GameObject as well

I could manually put in (because this works)

var gameobject1 : GameObject; 
var gameObject2: GameObject;

gameObject1.active = false;
gameObject2.active = false;

But then I would have a whole list of objects and i dont know how many will actually be in each room. So my question is how would I do this with an array of prefabs?

You could do it like this. This code is untested, but it should work.

var activeObjects : GameObject[];

function Start()
{
activeObjects.active = false;
}

function OnTriggerEnter(other : Collider) 
{
if(other.gameObject.tag == "myrobot")
{
for(var a = 0; a < activeObjects; a++){
activeObjects[a].active = true;
}
}
}
function OnTriggerExit(other : Collider) 
{
if(other.gameObject.tag == "myrobot")
{
for(var a = 0; a < activeObjects; a++){
activeObjects[a].active = false;
}
}
}

You have to access an array by index if you want just one element

activeObjects[×].active

Or you can iterate through all of them with

for ( var ao : GameObject in activeObjects ) {
ao.active = false;
}

Can’t you give the group a parent and disable that?

No, but you can use SetActiveRecursively().

–Eric

Well using the array does not seem to be working so I tried SetActiveRecursively() :smile: and it works! Which seems to be easier in the long run :slight_smile: Thanks a million you guys are alright!!!

Im almost starting to feel like a programmer, even though theres a long way to go lol

Woops here’s the script attached to the trigger, then create an empty gameobject and drag all of the items you want that trigger to be active when you enter it. Just in case someone else is needing the same type of thing. And in most cases instead of “myrobot” substitute your “Player”

var activeObjects : GameObject;

function Start()
{
	activeObjects.SetActiveRecursively(false);
}

function OnTriggerEnter(other : Collider) 
{
	if(other.gameObject.tag == "myrobot")
	{
		activeObjects.SetActiveRecursively(true);		
	}
}
function OnTriggerExit(other : Collider) 
{
	if(other.gameObject.tag == "myrobot")
	{
		activeObjects.SetActiveRecursively(false);
	}
}