Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Gameobjects: deleted (or invisible) in Hierarchy but accessible by code

Discussion in 'Editor & General Support' started by newerish, Feb 19, 2020.

  1. newerish

    newerish

    Joined:
    Sep 13, 2019
    Posts:
    11
    Hi,

    There are three gameobjects in my scene which are not shown in the Hierarchy panel. They are however listed by Resources.FindObjectsOfTypeAll(typeof(GameObject)) in the script.

    They have a messy origin. I loaded a Blender file into the scene, then copy and pasted three of its children into the hierarchy. Sometime later I deleted them all. I created separate Blender files for each and the loaded them into the scene separately. The gameobjects in question are the pasted three children in the beginning.

    My question is how can I get rid of the original three? Is there a way to cleanup the scene from a higher access than the Hierarchy panel?

    Thanks in advance.
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,135
    GameObjects are hidden in the hierarchy if their hideFlags include HideFlags.HideInHierarchy.

    If you add this code to your project you can use the menu item Tools/Reveal Hidden GameObjects to reveal any hidden GameObjects in your scene, allowing you to then delete them manually:
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public static class ToolsMenu
    6. {
    7.     [MenuItem("Tools/Reveal Hidden GameObjects")]
    8.     private static void RevealHiddenGameObjects()
    9.     {
    10.         var scene = SceneManager.GetActiveScene();
    11.         foreach(var gameObject in scene.GetRootGameObjects())
    12.         {
    13.             RevealHiddenGameObject(gameObject);
    14.         }
    15.     }
    16.  
    17.     private static void RevealHiddenGameObject(GameObject gameObject)
    18.     {
    19.         if(gameObject.hideFlags.HasFlag(HideFlags.HideInHierarchy))
    20.         {
    21.             Debug.Log("Revealing hidden GameObject "+gameObject.name, gameObject);
    22.             gameObject.hideFlags &= ~HideFlags.HideInHierarchy;
    23.         }
    24.  
    25.         foreach(Transform child in target.transform)
    26.         {
    27.             RevealHiddenGameObject(child.gameObject);
    28.         }
    29.     }
    30. }
     
    Last edited: Feb 19, 2020
    pitchblende likes this.
  3. newerish

    newerish

    Joined:
    Sep 13, 2019
    Posts:
    11
    Thank you but that was not the problem. I found the problem in my rookie brain :D. What happened was that I had renamed the prefab in the scene. So, the three gameobjects in the scene had different names from their source files. Meanwhile, the source files had the same names as the then-deleted pasted children. So, when I queried the gameobjects in the resources, it gave me those instantiated in the scene and their source prefab in the files, making me think the older ones still existed. I'd better use GetRootGameObjects. Sorry for taking your time.
     
  4. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,135
    Glad to hear you figured it out!

    Yeah using GetRootGameObjects is much safer than using Resources.FindObjectsOfTypeAll for finding all scene instances including inactivate and hidden ones, so you don't accidentally end up doing permanent changes to your prefab assets (not very fun).

    Resources.FindObjectsOfTypeAll is actually quite rarely useful given that AssetDatabase.FindAssets is the better solution for finding assets. The only time it really should be used is when finding instances that don't exist in the scene or in the assets folder (i.e. ScriptableObject instances, open EditorWindows).
     
    Gucci10C likes this.