Avoiding loading duplicate "dont destroy on load" objects when returning to a scene

I have several “global” objects in my MainMenuScene - things like the rewired input manager, fishnet’s network manager, etc. These are all “dont destroy on load” by default.

When the player quits out of the game (GameScene), the main menu scene gets loaded again - which causes duplication of any “dont destroy on load” objects. Most third party objects auto-destroy duplicates but it leaves me feeling like there’s a better solution here?

Is there a better way to manage global objects like these, without loading them a second time? Maybe an empty scene that’s always loaded or something?

What do you mean by “global”?

Things that need to be used in every scene. The rewired input manager, the fishnet network manager. Future things like an audio manager. Once the game starts, there needs to be 1 and only 1 of each, regardless of scene.

^ ^ ^ That’s the actual problem. Don’t do that. :slight_smile: Yes, every tutorial maker does it. Tutorial makers rarely ship production code.

Yes.

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

These are pure-code solutions, DO NOT put anything into any scene, just access it via .Instance

Alternately you could start one up with a [RuntimeInitializeOnLoad] attribute.

There is never a reason to drag a GameObject into a scene if it will be DontDestroyOnLoad.

If you do:

  • you may drag it into something else that isn’t DDOL. FAIL
  • you may drag something else into it that isn’t DDOL. FAIL

Just DO NOT drag anything into a scene that will be marked DontDestroyOnLoad. Just Don’t Do It!

Thanks but this isn’t what I was asking. I don’t control the scripts of third party assets. Both Rewired and FishNet already do this, but they warn in the console that “there can only be one of x game object”. This would never impact a build, but it caused me to consider whether there’s a better approach - because when the scene they begin in gets reloaded, it attempts to load duplicates.

I was thinking that maybe another scene that’s always loaded? Something that keeps them out of the main scene and will never get loaded as the user moves around menus/game scenes.

What causes them to load? If you’re placing them in the scene, that’s the problem.

Otherwise, use one of the constructs above and instantiate a prefab containing what you need, mark it as DDOL and… here’s the important part… don’t ever allow loading to happen again.

They’re in the MainMenuScene which is the first scene the game opens to. When the player begins a game, FishNet starts the server switches to the GameScene. When they quit the game, it unloads the GameScene and loads the MainMenuScene.

These “global” objects should be in an additive scene that gets loaded once at application start (or where it makes sense) and stays loaded throughout the application lifetime.

So rather than have them in your main menu scene, put them in another scene that gets additively loaded.

1 Like