Got error over singletons when unloading scene

The gentleman @Kurt-Dekker gave me a solution for a singleton usage/good practice here

This is a neat and clean solution, but sometimes I got an error in the editor when I stop playmode:

Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)

From what I understand it is due to how I manage (or how I DO NOT manage should I say) unloads.

    private static GlobalEvents _Instance;
    public static GlobalEvents Instance
    {
        get
        {
            if (!_Instance)
            {
                _Instance = new GameObject().AddComponent<GlobalEvents>();
                _Instance.name = _Instance.GetType().ToString();
                DontDestroyOnLoad(_Instance.gameObject);
            }
            return _Instance;
        }
    }

I did not do anything about that because so far the editor was handling himself well. But lately (from nowhere) it start showing me this error on one of my singleton and sometime all at once (it’s not concistent)

Should I fix it or is it not important (a.k.a just an editor problem)

Thanks

Well, did you do the above? That’s usually the issue…

Using OnDestroy() is difficult in Unity, specifically in the editor, because every time you press PLAY/STOP, everything gets destroyed and rebuilt.

Ideally use OnDisable() instead, but it is still possible to get this in editor.

If you can reason about it just being an editor thing fairly confidently (it likely is), then leave it. The alternative is lots of extra code that has to go on anything using singletons to decide if it is quitting right now or not, and honestly that kind of code causes more problems than it solves. I’ve seen teams chase this silly ghost for weeks trying to nail it down, and like I said, if it is only at the editor, don’t sweat it.

Ok, then I’ll pass it to tommorows me :smiley:

That’s the thing… I do not have any OnDestroy() method in any of my scripts o_o