# How can I find if a GameObject is not exist in the Hierarchy?

var selections = Selection.objects.OfType<GameObject>().ToList();
if (selections.Count > 0)
{
tempSelections = selections;

for (var i = selections.Count - 1; i >= 0; --i)
{
var selected = selections[i];
}

TransformSaver.SaveTransform(transformSelection.ToArray());
transformSelection = new List<Transform>();
}

for (var i = tempSelections.Count - 1; i >= 0; --i)
{
var selected = tempSelections[i];
if (GameObject.Find(selected.name) == null)
{
GUI.enabled = true;
}
else
{
GUI.enabled = false;
}
}
if (GUILayout.Button("Undo last selection if deleted"))
{
PlayerPrefs.DeleteKey("transform");
}


For testing I'm deleting one of the GameObjects in the tempSelections List

But then I'm getting exception in the editor on the line:

if (GameObject.Find(selected.name) == null)


Since the GameObject have been deleted it can't access the GameObject.

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

• Can you explain what are you trying to achieve? I didn't get it by reading your example. Are you trying to check if a GameObject you have in a cached list is still valid? – arielsan Aug 27 '18 at 0:38
• @arielsan I updated my question edited with the code. What I'm doing is in the class TransformSaver.SaveTransform I'm saving the selection of gameobjects to PlayerPrefs. And I want that if I deleted a GameObject in the Hierarchy for some reason and this GameObject is in the selection and also saved to the PlayerPrefs when I click the button "Undo last selection if deleted" re instantiate the deleted object. It's all working but the part of checking if the object hav been deleted or not. (Exist or not ). – BenBen Shmil Aug 27 '18 at 5:54
• @arielsan The idea is to make kind of Undo button for deleting object/s if by accident or if I deleted and want to bring it back. So after saving the selected objects to the PlayerPrefs I want that when I click the button it will re instantiate the deleted objects if I deleted some of them. So I need to check if this objects exist or not and only then to enable/disable the button. – BenBen Shmil Aug 27 '18 at 5:58
• @arielsan The code the first code with the GUILayout.Button is inside OnGUI and in EditorWindow type script. But the main goal is to check if object exist in the hierarchy not any object but only those in the selection List. Or in the PlayerPrefs. If one of them is missing not exist then enable true the button to be able to recover the object/s. – BenBen Shmil Aug 27 '18 at 6:00

The error message is pretty descriptive:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

(emphasis mine)

Your selections list contains 'zombie' objects that were already destroyed in the C++ engine part and thus it would be semantically incorrect to access them from the C# scripting engine part. Such 'zombie' GameObjects cannot be automatically 'deleted' in the C# part due to technical limitations and its up to you to handle them (drop the reference). Unity for convenience ensures that destroyed objects compare to null:

var selected = tempSelections[i];
if (selected == null) continue; //skip destroyed objects
//...

• ...which is how you check for deleted objects that you hold reference for ('destroyed but not forgotten') - that is what are you asking if I understand you right, find not please drop a comment specifying the problem you re facing. – wondra Aug 27 '18 at 8:29