How do I remove a Game Object that is not visible in the hierarchy?

I am receiving a console error (“The referenced script on this Behavior is missing!”). When I double click on it the associated game object is displayed and grayed out in the inspector but cannot be found in the Hierarchy or the Project screens. Likewise, I cannot find it when doing a search of the assets folder outside of Unity. I suspect the object is the result of removing a previously installed package and that there is an an xml file or something like that still referencing the object. Any ideas how to get rid of this object and error?

Here is a complete tool to inspect and get rid of hidden game objects easily.

using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.Collections.Generic;

public class HiddenGameObjectTools : EditorWindow
{
	#region Menu Command

	[MenuItem("Tools/Hidden GameObject Tools")]
	public static void ShowWindow()
	{
		var window = GetWindow<HiddenGameObjectTools>();
		window.titleContent = new GUIContent("Hidden GOs");
		window.GatherHiddenObjects();
	}

	#endregion

	#region GUI

	private static readonly GUILayoutOption ButtonWidth = GUILayout.Width(80);
	private static readonly GUILayoutOption BigButtonHeight = GUILayout.Height(35);

	private void OnGUI()
	{
		GUILayout.Space(10f);
		GUILayout.BeginHorizontal();
		{
			if (GUILayout.Button("Refresh", BigButtonHeight))
			{
				GatherHiddenObjects();
			}
			if (GUILayout.Button("Test", BigButtonHeight, ButtonWidth))
			{
				var go = new GameObject("HiddenTestObject");
				go.hideFlags = HideFlags.HideInHierarchy;
				GatherHiddenObjects();
			}
		}
		GUILayout.EndHorizontal();
		GUILayout.Space(10f);

		EditorGUILayout.LabelField("Hidden Objects (" + HiddenObjects.Count + ")", EditorStyles.boldLabel);
		for (int i = 0; i < HiddenObjects.Count; i++)
		{
			var hiddenObject = HiddenObjects*;*
  •  	GUILayout.BeginHorizontal();*
    
  •  	{*
    
  •  		var gone = hiddenObject == null;*
    
  •  		GUILayout.Label(gone ? "null" : hiddenObject.name);*
    
  •  		GUILayout.FlexibleSpace();*
    
  •  		if (gone)*
    
  •  		{*
    
  •  			GUILayout.Box("Select", ButtonWidth);*
    
  •  			GUILayout.Box("Reveal", ButtonWidth);*
    
  •  			GUILayout.Box("Delete", ButtonWidth);*
    
  •  		}*
    
  •  		else*
    
  •  		{*
    
  •  			if (GUILayout.Button("Select", ButtonWidth))*
    
  •  			{*
    
  •  				Selection.activeGameObject = hiddenObject;*
    
  •  			}*
    
  •  			if (GUILayout.Button(IsHidden(hiddenObject) ? "Reveal" : "Hide", ButtonWidth))*
    
  •  			{*
    
  •  				hiddenObject.hideFlags ^= HideFlags.HideInHierarchy;*
    
  •  				EditorSceneManager.MarkSceneDirty(hiddenObject.scene);*
    
  •  			}*
    
  •  			if (GUILayout.Button("Delete", ButtonWidth))*
    
  •  			{*
    
  •  				var scene = hiddenObject.scene;*
    
  •  				DestroyImmediate(hiddenObject);*
    
  •  				EditorSceneManager.MarkSceneDirty(scene);*
    
  •  			}*
    
  •  		}*
    
  •  	}*
    
  •  	GUILayout.EndHorizontal();*
    
  •  }*
    
  • }*

  • #endregion*

  • #region Hidden Objects*

  • private List HiddenObjects = new List();*

  • private void GatherHiddenObjects()*

  • {*

  •  HiddenObjects.Clear();*
    
  •  var allObjects = FindObjectsOfType<GameObject>();*
    
  •  foreach (var go in allObjects)*
    
  •  {*
    
  •  	if ((go.hideFlags & HideFlags.HideInHierarchy) != 0)*
    
  •  	{*
    
  •  		HiddenObjects.Add(go);*
    
  •  	}*
    
  •  }*
    
  •  Repaint();*
    
  • }*

  • private static bool IsHidden(GameObject go)*

  • {*

  •  return (go.hideFlags & HideFlags.HideInHierarchy) != 0;*
    
  • }*

  • #endregion*
    }

Because you are able to have the hidden object selected, you can just press Delete while that object is selected.

If you hadn’t been able to select it, you would have to write an Editor script to select it for you.

Try this:

using UnityEngine;
using UnityEditor;

public class Hider : EditorWindow {

	[MenuItem("GameObject_Hider&Destroyer/Start")]
	public static void Create(){
		GetWindow<Hider>();
	}

	void OnGUI(){

		if(GUILayout.Button("Create hidden GO")){
			GameObject h = new GameObject("hidden");
			h.hideFlags = HideFlags.HideInHierarchy;
		}
		if(GUILayout.Button("Select hidden GO")){
			Selection.activeGameObject = GameObject.Find("hidden");
		}

		if(GUILayout.Button("Destory Selected Object")){
			DestroyImmediate(Selection.activeObject);
		}
	}
}

I had the same issue, so I wrote this very simple script, and it works perfectly.

using UnityEngine;
using System.Collections;

// Makes a script execute in edit mode.

// By default, script components are only executed in play mode. 
// By adding this attribute, each script component will also have 
// its callback functions executed while the Editor is not in playmode.

// https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html
[ExecuteInEditMode]
public class makeVisible : MonoBehaviour 
{
	// The functions are not called constantly like they are in play mode.
	// - Update is only called when something in the scene changed.
	void Update () 
	{
		GameObject[] obj = (GameObject[]) GameObject.FindObjectsOfType(typeof (GameObject));
		foreach (GameObject o in obj)
			o.hideFlags = HideFlags.None; // https://docs.unity3d.com/ScriptReference/HideFlags.html
	}
}