- Home /
Is there a FindGameObjectsWithTag that returns active AND inactive objects?
Is there a FindGameObjectsWithTag that returns active AND inactive objects?
I want to toggle the active states of object when they are, you know, inactive... I can turn em false just fine, but no way to turn them true again.
I read that I need to keep everything in my own array but that seems like a major PITA and a fun waste of resources.
I assume when I run FindGameObjectsWithTag Unity is going through its internal array of game objects and returning a subset that match the tag and are active... can I get access to the internal array myself rather than keep a whole separate list on my own?
Answer by Kourosh · Apr 28, 2011 at 05:21 AM
I'm not sure if there's such a function. But I recommend to store only the inactive objects in an array (I just assume that the number of inactive objects are way less than the active ones so won't be a big performance cost for you.). The rest you can use the FineGameObjectsWithTag. that's also a quite heavy function.
I'm dealing with different zoom levels, planets, systems, universe. As it stands I'm creating everything up from but only some object are visible at different zooms. I was hoping to turn them on an off depending on the current zoom. I can turn them off just fine, but turning them on again is beco$$anonymous$$g problematic. In essence I need to keep all my objects in an array to turn them on an off as needed. this seems rather duplicative as Unity has the list already.
@Justin: It's just an array of references, not a big deal. It's less of a waste to store the references to the game objects with the desired tags and just iterate over that, rather than making Unity go through all game objects every time you want to turn some of them on or off.
@Eric5h5 - I guess its what I have to do, just seems like a massive oversite as far as the Unity API is concerned. Gotta rework all my object creation code to keep a unified list of references, something the engine, not the game, should to.
Depending on what else those objects are doing, you might just turn their renderers on/off as needed. Again, as long as any other behavior they may be doing won't impede performance or otherwise confuse the player, them being invisible and all..
Answer by AnXgotta · Jul 13, 2012 at 07:25 PM
Go to the MonoBehaviour reference at http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html
Here you will see all the functions that Unity will call for you based on certain events etc.
Take a look at the functions OnBecameVisible and OnBecameInvisible. It sounds like you might be able to use them.
If that doesnt work you could put the object you want to dynamically activate/deactivate and in an empty game object so it will be the child. Set the empty game object tag to what you search for and set the child active true or false. This will do what you want but it is a slight pain.