0
\$\begingroup\$
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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – arielsan Aug 27 '18 at 0:38
  • \$\begingroup\$ @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 ). \$\endgroup\$ – BenBen Shmil Aug 27 '18 at 5:54
  • \$\begingroup\$ @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. \$\endgroup\$ – BenBen Shmil Aug 27 '18 at 5:58
  • \$\begingroup\$ @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. \$\endgroup\$ – BenBen Shmil Aug 27 '18 at 6:00
4
\$\begingroup\$

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
//...
\$\endgroup\$
  • \$\begingroup\$ ...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. \$\endgroup\$ – wondra Aug 27 '18 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.