0
\$\begingroup\$

I've been looking around sample of a singleton gamemanager, like this from Unity's own page

if (instance == null)
        {
            instance = this;
        }
        else if (instance != this) // instance lost references
        {
            Destroy(gameObject); // this has the new references
        }

        DontDestroyOnLoad(gameObject);
}

I tried doing it the same way but after a LoadScene, the instance loses all of the GameObjects that I've referenced from the editor, which honestly makes sense to me (haven't used Unity much so I might have missed some lifecycle process or something) considering that everything else gets destroyed so the old instance would lose all of those reference while the new one, this, would have the fresh new references.

But also considering that nearly everyone is following this pattern and it seems to be working for them, what am I missing here?

I have no other code in an awake function, no coroutines and so- everything occurs right after the SceneLoad.

There is only one scene in the entire project and that's the scene that the game starts on and the scene that gets loaded after.

\$\endgroup\$
  • 1
    \$\begingroup\$ If an object that lives across scenes has dependencies, those dependencies should also be set up to live across scenes. Otherwise, you need a mechanism to wire-up and initialize fresh dependencies in the new scene. What that looks like will depend on what those dependencies are - can you show us what specific roles this manager's dependencies play? \$\endgroup\$ – DMGregory May 30 at 14:36
  • \$\begingroup\$ @DMGregory well, one example is the score gameobject from a canvas. So people either don't destroy the dependencies on load or they look for the new ones to attach? That makes more sense now. \$\endgroup\$ – kdyz May 30 at 23:38
  • \$\begingroup\$ I can think of lots of ways you might implement that. Please edit your question to show the way you did it in your project. \$\endgroup\$ – DMGregory May 30 at 23:39
  • \$\begingroup\$ @DMGregory that part's fine, I just asked this question to make sure that I wasn't missing some important lifecycle that somehow re attaches the new gameObjects or something. Thanks! \$\endgroup\$ – kdyz May 30 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.