- Home /
GameObject.Find() work on inactive objects
Does GameObject.Find() work on inactive objects? If not, how do you find and reference inactive objects.
i take it from that upvote this isn't going to be an easy solution? :-\
It seems like one of those strange design side effects that they don't have time or will to change. If you don't keep a reference to your inactive object it disappears in abyss of Unity's internal mystery.
I actually heard a rumor that they live happy lives there, free, with no one looking over their shoulders, no one trying to control them... they just keep dancing barefoot around fire in warm nights... But none of them ever returned to confirm that story. :-\
Here is example of one workaround: http://answers.unity3d.com/questions/14178/cannot-toggle-active-on-gameobjects-that-are-inactive
Please search Unity Answers and/or the forum, this question has been asked a gazillion times, and there have been many exhaustive answers given on how to find inactive objects. I remember giving a lengthy, detailed answer to at least one of these questions here on Unity Answers.
This lists several alternatives dealing with the problem: http://answers.unity3d.com/questions/125847/trouble-accessing-child.html
Answer by Antony-Blackett · Jun 03, 2011 at 04:51 AM
The solution only works if you don't rely on transform.root anywhere in your game. It also doesn't work if you need to find objects that are children of objects that don't get destroyed on load.
Add a GameObject that is the root of all GameObjects in your scene. Then instead of
FindObjectsOfType()
use
GetComponentsInChildren( typeof(Transform), true );
That should return all transforms of all GameObjects in the scene as they are all a child of the scene root, active or not.
If you can't or don't want to create a scene root object then you'll need to make an array of all the root objects in your scene and all GetCompoenentsInChildren() on all of them separately.
nice hack, but it's still a cheesy workaround for a missing feature that ought to be included :-p
And if you are wanting to find an inactive by name, you can do this:
Text[] texts = m_rewardPopup.GetComponentsInChildren<Text>(true);
Text plus = System.Array.Find(texts, (search) => (search.name.Equals("plus", System.StringComparison.Ordinal)));
For more u can refer http://www.unityrealm.com/how-to-find-inactive-gameobject-in-unity/
Answer by Antony-Blackett · Dec 05, 2011 at 04:08 AM
Here's how you do it.
Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
Note this can also return prefabs not just object instances in the scene so be careful when using it at edit time.
Answer by bdawson · Oct 31, 2012 at 09:30 PM
This is kind of gross, but it works well:
public static List<GameObject> GetAllObjectsInScene(bool bOnlyRoot)
{
GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));
List<GameObject> pReturn = new List<GameObject>();
foreach (GameObject pObject in pAllObjects)
{
if (bOnlyRoot)
{
if (pObject.transform.parent != null)
{
continue;
}
}
if (pObject.hideFlags == HideFlags.NotEditable || pObject.hideFlags == HideFlags.HideAndDontSave)
{
continue;
}
if (Application.isEditor)
{
string sAssetPath = AssetDatabase.GetAssetPath(pObject.transform.root.gameObject);
if (!string.IsNullOrEmpty(sAssetPath))
{
continue;
}
}
pReturn.Add(pObject);
}
return pReturn;
}
Isn't AssetDatabase an editor class? $$anonymous$$eaning this script doesn't work as a runtime solution?
Its true. You don't need that check unless you are in the editor. I will update.
Answer by cregox · May 14, 2013 at 08:48 PM
There are at very least 4 options: `GetChild`, `GetComponentsInChildren`, `FindObjectsOfType` and `FindGameObjectsWithTag`. We could also use some variations that won't bring an array, for instance. Listed below in no particular order.
GetChild a method of
Transform
, it still works even today on Unity4, and it's still not in the Docs for whatever reason. It will bring the child be it active or not. Probably the cheapest to use, since we have to use in each immediate parent we want to get the inactive GameObject. It will just be a pain to code and maintain.GetComponentsInChildren needs to have a root object. We could set up the scene with just 1 main root here. Very simple to use, but also very heavy. Apply this in the root object:
foreach (Transform child in GetComponentsInChildren(true))`
FindObjectsOfType doesn't need a root object. Docs say it won't return inactive objects, but it does. Since it's the heaviest listed here and highly unadvised to use, we could do something like:
foreach ( GameObject root in GameObject.FindObjectsOfType(typeof(GameObject)) ) { if (root.transform.parent == null) { // game object with no parent // here, iterate through each `root` child using your prefered method // or simply remove the 'if' above } }
FindGameObjectsWithTag my favourite one, way faster and simpler than all others, but we can't add more than 1 tag per object so it may be prohibitive if we already use tag for something else. It also needs no root, use it from anywhere:
foreach ( GameObject obj in GameObject.FindGameObjectsWithTag("tag") )
Disclaimer: @bdawson actually gave the same solution about FindObjectsOfType and @Antony scratched it, twice. None were specific enough.
in the current version of Unity (2017) FindObjectsOfType correctly DOES NOT return inactive objects. (Unfortunately!)
In Unity 2017 "FindGameObjectsWithTag" doesn't find inactive objects.
Answer by Noob_Vulcan · Aug 13, 2015 at 08:18 AM
Things that can find inactive gameObjects :
transform.Find() or transform.FindChild()
transform.GetComponentsInChildren(true)
Resources.FindObjectsOfTypeAll()
For more detail you can refer to http://www.unityrealm.com/how-to-find-inactive-gameobject-in-unity/