x


Cannot toggle active on gameobjects that are inactive

This problem is giving me headaches and is taking a while to figure out, can you guys help me out. I posted a related problem a few days ago that worked, but only for making objects inactive. Now that they are inactive, whenever I try to use the GameObject.FindGameObjectsWithTag() function, it cannot find the inactive gameobjects.

Here's my code:

static var time = 0.0;
static var timerOn = false;
static var exploring : boolean;
static var zones : GameObject[];

function Start()
{
  exploring = false;
  var zones = GameObject.FindGameObjectsWithTag("zones");
  //var zones = GameObject.FindGameObjectsWithTag("zones");
  //exploring = true
  exploring = true;
}

function Update ()
{ 
  if (timerOn == true)
      {
          time = time+1*Time.deltaTime;

          //Debug.Log (time);
      }
  if (exploring == false)
  {
      //for(var go : GameObject in GameObject.FindGameObjectsWithTag("zones"))
      for(var go : GameObject in GameObject.FindGameObjectsWithTag("zones"))
      {
          go.active = true;
      }
  }
  if (exploring == true)
  {
      for(var go : GameObject in GameObject.FindGameObjectsWithTag("zones"))
      {
          go.active = false;
      }
  }
}

I've messed around with it quite a bit, so sorry for all the comments, and weird stuff in the start function.

Thanks for any help!

more ▼

asked Jul 01 '10 at 01:56 PM

Fusobotic gravatar image

Fusobotic
89 4 4 9

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

2 answers: sort voted first

What you need to do is maintain a list of inactive objects so you don't need to use find with tag all the time which is what your problem is.

When you deactivate them do something like this:

var objArray;

function Start(){
   objArray = new Array;
}

... your bit ...

if (exploring == true)
  {
      for(var go : GameObject in GameObject.FindGameObjectsWithTag("zones"))
      {
          objArray.Push(go);
          go.active = false;
      }
  }

Then when you want to bring them back on do this:

while(objArray.length>0){
   var go : GameObject = objArray.Pop();
   go.active = true;
}

Array docs:

http://unity3d.com/support/documentation/ScriptReference/Array.html

more ▼

answered Jul 01 '10 at 02:12 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

Yes that works perfectly! Thank you!

Jul 01 '10 at 03:04 PM Fusobotic

Glad I could help

Jul 01 '10 at 07:34 PM spinaljack
(comments are locked)
10|3000 characters needed characters left

Another approach would be to selectively disable the components that a gameObject have, instead of disabling the gameObject. This approach lets you access again the gameObject via the methods you mentioned: GameObject.FindGameObjectsWithTag()

Suppose that you have a gameObject with a Mesh Renderer and a script called myScript. You can set those two object to a disabled state:

renderer.enabled = false;
GetComponent.<myScript>.enabled = false; 

Colliders are more tricky. You can disable them by setting the gameObject in a different layer eg 10.

gameObject.layer = 10;

You should then define in the Physics inspector that objects in layer 10 don't collide with other objects. Physics inspector can be found in Menu->Edit->ProjectSettings->Physics and the place you define the collisions is the Layer Collision Matrix.

Notice that for perfomance reasons it seems to be a good practice to disable all scripts that contain onGui functions when you don't use them.

GetComponent.<myGuiScript>.enabled = false;

A good explanation on why you should do this is here http://www.mindthecube.com/blog/2010/09/avoiding-performance-cost-of-ongui

References:

http://unity3d.com/support/documentation/ScriptReference/GameObject-layer.html

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

http://unity3d.com/support/documentation/ScriptReference/Behaviour-enabled.html?from=MonoBehaviour

(since scripts are MonoBehaviours that descent from Behaviours so they inherit their variables)

http://unity3d.com/support/documentation/ScriptReference/Renderer-enabled.html

more ▼

answered Feb 04 '11 at 03:16 PM

Ippokratis gravatar image

Ippokratis
449 2 6 18

I'm having trouble defining the collision parameters for a specific layer (layer 10 in your example) to turn off collisions... Could you elaborate?

Apr 07 '12 at 03:39 PM valichm
(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:

x162
x114
x7

asked: Jul 01 '10 at 01:56 PM

Seen: 7152 times

Last Updated: Apr 07 '12 at 03:39 PM