Question by
alexanderameye · Nov 15, 2016 at 03:20 PM ·
errormissingreferenceexceptionmissing-referencedestroyimmediate
Unity error The object of type 'Preview' has been destroyed but you are still trying to access it.
I have this script
using UnityEngine;
using UnityEditor;
[CustomEditor( typeof(DoorPro) )]
public class DoorProEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector ();
//Colors
Color red = new Color(1F, 0F, 0F, 1F);
Color green = new Color(0F,1F,0F,1F);
bool PreviewActive = false;
if(Selection.activeGameObject.GetComponent<Preview>() == null) PreviewActive = false;
else PreviewActive = true;
if(PreviewActive == false) GUI.color = green;
if(PreviewActive == true) GUI.color = red;
if(GUILayout.Button("Preview Mode"))
{
if(PreviewActive == false)
{
Selection.activeGameObject.AddComponent<Preview>();
}
else
{
DestroyImmediate(Selection.activeGameObject.GetComponent<Preview>());
}
}
}
And it runs fine, but I still get the error:
MissingReferenceException: The object of type 'Preview' has been destroyed but you are still trying to access it.
Where did I go wrong?
Comment
Your answer
Follow this Question
Related Questions
Why does Audiosource.PlayClipAtPoint gives me a MissingReferenceException when loading a new scene? 0 Answers
Missing Reference Exception... please help, I am new to this! 2 Answers
Weird MissingReferenceException 0 Answers
Destroying GameObject while in Inspector = MissingReferenceException 0 Answers
Unity Firebase Google SignIn Error Help 0 Answers