- Home /
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?
Answer by Jamora · Jan 11, 2014 at 09:14 AM
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);
}
}
}
@Jamora,
The script did the trick. I could not select the object as it was grayed out. However, I could see the name of the object and using Selection.activeGameObject = GameObject.Find("GO Name") allowed me to select it DestroyImmediate took care of the rest.
Thank you
Please do not post comments as answers.
If this answer answered your question you should accept. If you don't know how, watch the tutorial video and read the guidelines in the FAQ on the right.
Answer by Can-Baycay · Oct 27, 2016 at 10:31 PM
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[i];
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<GameObject> HiddenObjects = new List<GameObject>();
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
}
Answer by lyzzard · Nov 20, 2016 at 08:16 PM
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
}
}
Your answer
Follow this Question
Related Questions
Shooting an object through a tube of cannon in a projectile manner. 0 Answers
How do I Adjust The Colour Of My Object By Percentages? 1 Answer
Object from flash to unity 2 Answers
under water fog 2 Answers