How to persist global GameObject across scenes and keep individual scenes testable?

I want my game to have multiple scenes, where each menu and each game level will be in scene of it’s own. The major reason is to be able to run and test each part of the game independently.

I’ve read this tutorial which explains how to use DontDestroyOnLoad to persist GameObjects across the scenes: Implement data persistence between scenes - Unity Learn
However, the problem is that I still need to test my game by starting it from the scene where the object is created.

For example: let’s say that I have GameManager singleton in MainMenu scene and DontDestroyOnLoad is called to persist it. Still, it wont be possible to test Level1 scene without running MainMenu scene first so that GameManager get’s created.

Is there some design pattern that solves this issue? I want that GameManager GameObject get created once for the entire game, regardless of which scene I have started first.

Many ways. One, does it need to be a game object? Could it be a scriptable object? Or even just a static class? Is the singleton even the right pattern here to begin with?

You could also make it lazily initialise itself when first accessed, such as loading itself through Resources.

Notably none of these require it to be in its own scene. So when you say “I want my game to have multiple scenes”, I will say additive scenes are great, but they aren’t appropriate in every situation.

My personal approach for managers (for want of a better term) that require being a game object (which isn’t often the case, I find), is to just have themselves lazily initialise when first accessed. Otherwise most are just scriptable objects or static classes.

Thanks Spiney.

For GameManager I don’t actually need GameObject. I was using GameObject to locate GameManager component but really that’s not necessary and I could just use static methods to for initialization and obtaining a reference.

Can you expand on your LoadMode.Additive idea? Did you mean to load common GameObjects in individual scenes for each level? Would this create some issues if some of these objects hold references to a lot of resources (common for all levels)? My worry was that all resources would get reloaded and therefore slow down scene transitions.

I wouldn’t use LoadMode.Additive for GameManager but I do have some other common GameObjects which do hold a lot of resources and do need tome Unity components. It seems that LoadMode.Additive might be appropriate for such cases.

I never mentioned anything about LoadMode.Additive so I have no idea what you’re going on about here.

If you don’t want to lazy load as Spiney discussed, you can use the RuntimeInitializeOnLoadMethod attribute to launch code when you press play, or when the built player starts running. I recommend RuntimeInitializeOnLoadType.BeforeSceneLoad because that is when you can successfully instantiate game objects that won’t get automatically destroyed, but before any Awake() calls for objects in the initial scene.

2 Likes

The above attribute actually lets you make Unity projects with ZERO scenes.

https://gist.github.com/kurtdekker/98227547b84506edbbfd9d7d2ae33543

This approach is ideal for the purist / masochist who wants to stay in the 1980s but still have access to an otherwise modern game engine.

So you mentioned additive scenes. I did a quick search and noticed that there is LoadSceneMode.Additive…

Yes, what Spiney (barely) alluded to was broadly called Additive scenes.

Additive scenes serve a purpose. The are not always appropriate, as Spiney pointed out.

Here’s some more skinny:

Additive scene loading is one possible solution:

https://discussions.unity.com/t/820920/2
https://discussions.unity.com/t/820920/4

https://discussions.unity.com/t/824447/2

A multi-scene loader thingy:

https://pastebin.com/Vecczt5Q

My typical Scene Loader:

https://gist.github.com/kurtdekker/862da3bc22ee13aff61a7606ece6fdd3

Other notes on additive scene loading:

https://discussions.unity.com/t/805654/2

Timing of scene loading:

https://discussions.unity.com/t/813922/2

Also, if something exists only in one scene, DO NOT MAKE A PREFAB out of it. It’s a waste of time and needlessly splits your work between two files, the prefab and the scene, leading to many possible errors and edge cases.

Two similar examples of checking if everything is ready to go:

https://discussions.unity.com/t/840487/10

https://discussions.unity.com/t/851480/4

1 Like

Kurt thanks for all the pointers on additive loading. I plan to use some combination of singleton objects (without GameObject) + LoadMode.Additive.