Object remain is scese after playing in editor

I have a Game Manager class that is a singleton. I am having a problem where if I play the game in the editor, it creates the GameManager, but when I stop it, the GameManager remains in the scene. If I play it again it will create another one over and over. I then have to manually go and select and delete them. I thought that having it a singleton meant that there would never be more that one of them? Does anybody know why this would happen?

also, it doesn’t always happen either. its 50/50.

here’s the code I am using for my class

static GameManager instance;

    // Use this for initialization
    public static GameManager Instance
    {
        get
        {
            if (!instance)
            {
                // check if an ObjectPoolManager is already available in the scene graph
                instance = FindObjectOfType(typeof(GameManager)) as GameManager;

                // nope, create a new one
                if (!instance)
                {
                    GameObject obj = new GameObject("GameManager");
                    instance = obj.AddComponent<GameManager>();
                }
            }

            return instance;
        }
    }

    void OnApplicationQuit()
    {
        instance = null;
    }

Thanks

-John[/code]

That is because you create a new GameObject. I’m not sure why it stays in the scene, but I guess you’re using DonDestroyOnLoad(this)?? It shouldn’t keep the gameobject in the scene though, but maybe a bug?

What you need to do is to have a GameObject with the GameManager component attached to it in the scene before starting.

//if the object is already in the scene, then this will find the instance. 
instance = FindObjectOfType(typeof(GameManager)) as GameManager; 

                // dont create one. do this before starting
                if (!instance) 
                { 
//Change the lines below to throw an exception or log an error. 
                    //GameObject obj = new GameObject("GameManager"); 
                    //instance = obj.AddComponent<GameManager>(); 
                }

Hope this helps.

No, I’m not using DoNotDestroy(this). I’m also creating a whole bunch of other gameobjects when the scene starts up. None of those remain in the scene. Only this one. The others are just prefabs being instantiated at start. This one is a singleton, I don’t know if that is causing the problem or not.

Is anybody else using a Singleton experiencing this?

also, even if I leave the object in the scene and play it again, it will still create a new one, so if I play it 5 times I end up with 5 of them in the scene.

EDIT**

ok, i figured out what is causing the problem, but I’m not sure how to fix it. I have other objects in the scene which reference the GameManager when OnDisable gets called. I guess it’s getting called when I stop playing in the editor and that causes my problem. I still need it to be called when it’s disabled while playing though. So how can I stop it from being called when I stop playing in thee editor. Is there something I can check before running the code inside the OnDisable funtion?

void OnDisable()
    {
        if (GameManager.Instance.breakableBallsInScene < 1)
            GameManager.Instance.winGame();
    }

I changed my OnDisable code to this and it seemed to fix it

void OnDisable()
    {
        if(FindObjectOfType(typeof(GameManager)))
        {
            if (GameManager.Instance.breakableBallsInScene < 1)
                GameManager.Instance.winGame();
        }
    }