Singleton and DonstDestroyonLoad Problem

I have a Game manager setup which has bunch of tasks like score, changingscene, and GameManager Script

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

	static GameManager _instance;
	public static GameManager managerInstance
	{
		get
		{
			if(_instance == null)
			{
				_instance = GameObject.FindGameObjectWithTag("manager").GetComponent<GameManager>();
				//Tell unity not to destroy this object when loading a new scene!
				DontDestroyOnLoad(_instance.gameObject);
			}
			return _instance;
		}
	}

	void Awake()
	{
		if(_instance == null)
		{
			//If I am the first instance, make me the Singleton
			_instance = this;
			DontDestroyOnLoad (this);
		}
		else
		{
			//If a Singleton already exists and you find
			//another reference in scene, destroy it!
			if(this != _instance)
			Destroy(this.gameObject);
		}
	}

}

Now In sceneA i have this manager placed and when i move from sceneA to sceneB the gamemanager is still there

but as i start sceneB directly i get various errors regarding object being null ( clearly the manager isnt there )

So my question is

"is it ok to use game manager object placed in multiple scene, Instantiating them when we need and make them persist if we want them to move from one scene to another ? "

isnt there any centralised process from which i can handle my game ?

It will be a problem if i attempt to start game directly in sceneB if game is Over ( sceneA is Menu and options )

how can i start game in between with persistence manager object ?

I’d recommend not placing the game manager object in the scene. Instead, generate the game manager and the GameObject it’s attached to (if it needs a game object for Update and FixedUpdate calls) in the getter:

static GameManager _instance;
public static GameManager instance {
    get {
        if (_instance == null) {
            GameObject manager= new GameObject("[GameManager]");
            _instance = manager.AddComponent<GameManager>();
            DontDestroyOnLoad(manager);
        }
        return _instance;
    }
}

That allows you to not use the check for null in awake - the manager always exists if some outside source has needed it.

By the way, if your game manager doesn’t need Update or any of the other built-in MonoBehaviour methods, don’t make it a MonoBehaviour, just leave it as a normal class, and generate a singleton. Static objects that are not MonoBehaviours attached to GameObjects doesn’t get destroyed on scene loads. If that’s the case, you can just set up a normal C# singleton:

public class GameManager {

    private GameManager _instance;
    public GameManager instance {
        get {
            if(_instance == null)
                _instance = new GameManager();
            return _instance;
        }
}

And you’re all set.

I think that your problem may be that you are referencing the GameManager object on the scene A, rather than finding the current instance:

  1. In your SceneA your button contains a reference to the GameManager on that scene. A reference assigned through the Unity Editor to the button object, correct?
  2. The first time it works. But when you return to SceneA after playing, the new SceneA contains its own GameManager (referenced by the new button) which will be destroyed because there is already a singleton instance.

Instead of trying to assign the GameManager object with the Editor, use functions to find the current instance when you need it.