Accesing a singleton from a custom editor script (NullReferenceException)

So, I’m creating a custom editor window and a custom Inspector and both need to access the value of something inside a singleton I call LevelController, but on the custom EditorWindow script it’s giving Null Reference Exception that I can’t understand why it’s happening. Here’s my simple humble singleton I just made:

It contains a list of Triggers inside a list of Levels and a current level counter integer.

public class LevelController : MonoBehaviour
{
    [System.Serializable]
    public class TriggerClass
    {

        public GameObject Trigger;
        public bool Active;
        public bool Occupied;
        public int Type;

        public TriggerClass(GameObject obje, bool active = false, int type = 1, bool occupied = false)
        {
            Occupied = occupied;
            if (!Occupied)
            {
                Trigger = obje;
                Active = active;
                Type = type;
            }
        }
    }

    [System.Serializable]
    public class WrapperClass
    {
        public List<TriggerClass> Triggers = new List<TriggerClass>();
    }

    public static LevelController ThisInstance = null;

    public List<WrapperClass> Levels = new List<WrapperClass>();

    public int CurrentLevel = 0;

    void Awake()
    {
        if (ThisInstance != null)
        {
            GameObject.Destroy(gameObject);
        }
        ThisInstance = this;
        DontDestroyOnLoad(ThisInstance);

    }
}

I guess the important part is the Awake() but I put everything as it’s small.

Now, I’m not gonna post the entire EditorWindow script as it’s huge, but everytime I try to access my singleton, for exemple in a for loop to assign the name of the triggers on a current level to an array of strings called options:

for (int i = 0; i < LevelController.ThisInstance.Levels[LevelController.ThisInstance.CurrentLevel].Triggers.Count; i++)
        {
            options[i] = LevelController.ThisInstance.Levels[LevelController.ThisINstance.CurrentLevel].Triggers[i].Trigger.name.ToString();
        }

or just plain access to a variable

int a = LevelController.ThisInstance.CurrentLevel;

it gives (actually it spams) NullReferenceException: Object not set to an instance of an object
altough I did put the singleton in Awake() instead of Start(), LevelController IS in an object on the scene and I messed around the script execution order, and funny enough, in my custom inspector script, it accesses the singleton perfectly.

Any ideas, please?
I’ve spent hours searching the web and found absolutelly no answer.

What I think is happening is your gameobject is being destroyed but your script is being assigned to the value.
I might be completely wrong on this. But, I think what is happening is the first time you come to the scene, it’s assigning a value to ThisInstance. However, the second time you come back to the scene, it says, oh, I have a value, destroy the gameobject in the scene that has this script attached, however, take this script and assign it to the static ThisInstance. So now you are no longer referencing the script on the floating gameobject, but a script on a gameobject that was destroyed.

Now, this is just a guess and may be incorrect, however I think if you put your

ThisInstance = this;
        DontDestroyOnLoad(ThisInstance);

into an else statement, that may fix the issue for you. Just a thought.