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.
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
For more ways you can refer this UnityRealm.com is for sale | HugeDomains
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.
just had this problem. Of course, I use registrires, singletons, awake instance setting, properly developed editors, etc.
My use case was that I use this asset from the asset store : Unity Asset Store - The Best Assets for Game Making and it doesn’t find things for me that are inactive.
I wrote the following methods, which resolves the problem. Note - I also added some simple caching to the shelf (why asset store authors are happy to write code that slows our editor down to crap is beyond me) - anyone feel free to message me if they want the improved method for theshelf, if you can’t work it out for yourselves.
public static Transform FindInAnyScene(string[] path)
{
Transform matchedTransform = null;
for (var i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);
foreach (var rootGameObject in scene.GetRootGameObjects())
{
matchedTransform = FindAnywhere(rootGameObject.transform, path);
if (matchedTransform != null)
{
break;
}
}
}
return matchedTransform;
}
public static Transform FindAnywhere(Transform current, string[] path, int currentDepth = 0)
{
var targetName = path[currentDepth];
var matchedTransform = current.name == targetName ? current : null;
if (matchedTransform != null && path.Length - 1 > currentDepth)
{
var children = matchedTransform.GetComponentInChildren<Transform>(true);
foreach (Transform child in children)
{
matchedTransform = FindAnywhere(child, path, currentDepth + 1);
if (matchedTransform != null)
{
break;
}
}
}
return matchedTransform;
}
I found that the following works great (at least for my editor scripts, and while using a single scene):
- Get all the Root GameObjects in the Active Scene (this also finds inactive Root GameObjects).
- For each of those GameObjects, get all Transforms on itself and it’s children, specifically requesting to include those of inactive GameObjects.
- This leaves you with a List of the Transforms of literally every GameObject in the Active Scene, regardless if those GameObjects are active or not, so you can now do any remaining operations on that instead.
For example, I’ve included a function that uses the list to find either all locally inactive GameObjects (i.e. in Inspector, their Active checkbox must be false), or globally inactive GameObjects (i.e. the GO itself, or any of it’s (grand)parents, is inactive => any GameObject that is “greyed out” in Hierarchy):
private static List<Transform> GetAllTransformsInActiveScene()
{
List<Transform> allTransformsInSceneIncludingInactive = new List<Transform>();
GameObject[] sceneRootGameObjects = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
Debug.Log(sceneRootGameObjects.Length + " Root GameObjects found in Scene.");
foreach (GameObject go in sceneRootGameObjects)
{
Transform[] childTransforms = go.GetComponentsInChildren<Transform>(includeInactive: true);
foreach (Transform tChild in childTransforms)
{
allTransformsInSceneIncludingInactive.Add(tChild);
}
}
return allTransformsInSceneIncludingInactive;
}
private static List<GameObject> FindAllInactiveGameObjects(bool locallyInactiveOnly)
{
List<Transform> allTransformsInScene = GetAllTransformsInActiveScene();
List<GameObject> allInactiveGameObjectsInScene = new List<GameObject>();
foreach (Transform t in allTransformsInScene)
{
if (locallyInactiveOnly)
{
//the GameObject itself is locally inactive, i.e. in Inspector, it's Active checkbox is "false"
if (t.gameObject.activeSelf == false)
{
allInactiveGameObjectsInScene.Add(t.gameObject);
}
}
else
{
//the GameObject itself, or any of it's (grand)parents, is inactive
if (t.gameObject.activeInHierarchy == false)
{
allInactiveGameObjectsInScene.Add(t.gameObject);
}
}
}
return allInactiveGameObjectsInScene;
}
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);
}
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();