- Home /
Find inactive game object.
Hi guys, is there some way to find inactive game object? I don't want to use drag and drop in editor and GameObject.FindWithTag finds only active game objects. Is there some way I don't know about or do I have to set them active at start and deactivate them later.
i actually reposition to some far out of the way place rather than inactive. Solved the issue for me.
yeah I am thinking about active at start off the screen, then deactive and change position where I need, but would have been easier I could find inactive objects with GameObject.FindWithTag
you can add objects into one list before deactivate and active them again
Answer by JiMMaR · Mar 17, 2015 at 04:58 PM
As far as I know, you can't.
you can however start as active and reference them in your code before you deactivate them.
you could also try referencing their parent [if it was a transform] and then iterate over the children if that's not a bad thing in your case
Thanks, I did the first one (active at start and deactivate later) , I have read some older posts from 2007-2013 where people suggested there should be way to find inactive objects and I wanted to be sure. I guess it would make some mess if we could do that with GameObject.FindWithTag.
You could also start as active, and when deactivating, add to a "deactivated list" - or a grosser way would have all game objects be children of a "root" parent, and then you can use GetComponentsInChildren (which has an additional includeInactive
argument)
thanks for this! I couldn't figure out how to get object information from objects I had instantiated and were by default inactive due to it's parent. i created the objects in awake, and then turned the parent object on in awake after i created them so that my other script could get the data from them in it's start function. then i disabled the parent object on start..
Answer by darkhog · Jan 07, 2016 at 05:20 PM
If anyone will have this issue, here's what I've used to resolve this problem:
GameObject filenamefld = null;
Transform[] trans = GameObject.Find("EditorGUI").GetComponentsInChildren<Transform>(true);
foreach (Transform t in trans) {
if (t.gameObject.name == "FileNameFLD") {
filenamefld = t.gameObject;
}
}
You may want to put that into its own function, but since I've used it only in one place, this is sufficient. EditorGUI is "root object" and FileNameFLD is object we actually want to find. Hopefully this is useful.
I don't think this will find nested inactive children, try and confirm also you would probably want to break out of the loop after you find your object
Or use a one-liner:
GameObject.Find("ActiveParentsName").transform.Find("InactiveChildsName").gameObject;
Answer by Noob_Vulcan · Jul 25, 2015 at 05:54 AM
For more ways you can refer this http://www.unityrealm.com/how-to-find-inactive-gameobject-in-unity/
Answer by Sw_Architect · Apr 13, 2018 at 07:28 AM
first of all active the object and get in Awake() then simply disable it in Start();
e.g
void Awake(){ GameObject game=getcomponent();
} void Start(){ game.setActive(false); }
Answer by badadam · Jan 14, 2019 at 02:15 PM
I give you extraordinary solution. Add the script below to your inactive object as a component.
public class InactiveObject : MonoBehaviour {
public static InactiveObject instance;
public InactiveObject()
{
instance = this;
}
public void makeActive()
{
gameObject.SetActive(true);
}
}
When you want to set the game object active, you can run the code below from another script
InactiveObject.instance.makeActive();
This is a really bad "solution". For starters, it would require a separate class of this sort being created for each individual object that you wanted to be able to find.
Also, you're giving a $$anonymous$$onoBehaviour class a constructor. Don't do that!