- Home /
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!
Answer by spinaljack · Jul 01, 2010 at 02:12 PM
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
Answer by Ippokratis · Feb 04, 2011 at 03:16 PM
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
I'm having trouble defining the collision parameters for a specific layer (layer 10 in your example) to turn off collisions... Could you elaborate?