Typically when running a scene, you’d determine if a gameobject is inactive by using gameobject.activeself.
However, this doesn’t seem to work when checking objects that are nested in prefabs within my files (meaning I’m not running the game and the prefab is not within my scene view, just in my files). gameobject.activeself will always return true in this case, even if the active checkbox has been unchecked for that gameobject. Is there some alternative method of checking this that I’m not aware of?
Um what? All GameObjects in a prefab are Inactive. Instantiate the prefab and its objects become active but its no longer a prefab; its in the hierarchy.
https://forum.unity.com/threads/no-gameobject-active-in-a-prefab.12298/
I’m not sure if this is what you are looking for. This piece of code enumerates each and every gameobject in a given prefab with the information about whether it is active or not (GameObject.activeSelf). Paste that code somewhere in your script. It will add menu item “Test/Test” to the menu bar in the editor. You have to specify the location of your prefab and put it in the variable “path”. In my example I assumed that the prefab1.prefab is in the Assets folder.
string path = "Assets/prefab1.prefab";
The code works like this: it clears the console. Next, it instantiates the prefab and enumerates gameobjects with their corresponding local active state in the console. Afterwards, it destroys the instantiated object of your prefab.
[UnityEditor.MenuItem("Test/Test")]
private static void Test()
{
System.Type logEntries = System.Type.GetType("UnityEditor.LogEntries, UnityEditor.dll");
System.Reflection.MethodInfo clear = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
clear.Invoke(null, null);
string path = "Assets/prefab1.prefab";
Object prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
if (prefab==null)
{
Debug.Log("There is no prefab at the specified location.");
return;
}
GameObject g = Instantiate(prefab as GameObject);
Rec(g.transform);
GameObject.DestroyImmediate(g);
}
private static void Rec(Transform t)
{
Debug.Log(t.name + ": " + t.gameObject.activeSelf);
int length = t.childCount;
for (int i = 0; i < length; i++)
{
Rec(t.GetChild(i));
}
}