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];
transformSelection.Add(selected.transform);
}
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"))
{
transformsCount = TransformSaver.LoadTransform().Length;
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.