GameManager in unity 5.4

In unity Docs:

“It is recommended to avoid using DontDestroyOnLoad to persist manager GameObjects that you want to survive across scene loads. Instead, create a manager scene that has all your managers and use SceneManager.LoadScene(path, LoadSceneMode.Additive) and SceneManager.UnloadScene to manage your game progress.”

So, i created a “GameManagers” scene and called SceneManager.LoadScene(“GameManagers”, LoadSceneMode.Additive) and it loads my “GameManagers” scene, but i don’t know what to do now.

How can i reference a GameManager gameobject from that scene?

You could use SceneManager.GetSceneByName(“sceneName”).GetRootGameObjects() to get an array of root gameobjects inside the scene.

So basically something like:

GameObjects gameObjects = SceneManager.GetSceneByName("sceneName").GetRootGameObjects();
GameManager gameManager;

foreach(GameObject go in gameObjects)
    if(go.tag == "GameManager")
        gameManager = go.GetComponent<GameManager>();

If you only have one gameObject in the scene, then you could just reference gameObjects[0] without the foreach loop.

I understand the idea of using a Scene with a GameManger object. It is definitely easier to keep the project clean this way. Personally if i’m going with a small project, i will definitely use DontDestroyOnLoad with a GameManager object between scenes.