Avoiding The Singleton Pattern - persistent objects alternative

Just my opinion. I think using different Unity Scenes make the most sense when you don’t need to persist things between them. In this example, moving between different Areas in a map, might make more sense to load/unload areas within the one Scene.

Are you referring to Additive Loading also known as level streaming? This is definitely a possible approach, though it comes with it’s own set of issues. For example, you would need to take precautions to ensure that an enemy didn’t cross over scene boundaries. If you didn’t, it could be possible for an enemy to continue to engage over multiple “scenes” (like if the player was kiting the enemy), and when the player got far enough away from the enemy’s home scene, the enemy would end up being unloaded.

I was saying for the one area at a time model. You could still fade out/ fade in, but instead of changing scenes you would use prefabs for the different areas. But yeah if you were streaming levels then you would need to persist the player and camera so would be even more objects to persist between scenes.

The same caveats would apply using a prefab approach, at least in terms of ensuring that enemies don’t have their parent GameObjects destroyed out from under them.

Hi, I just realized I didn’t explain my idea well. I created a scene hierarchy to try to explain better.


The idea is that the area is a prefab instead of a scene, so when you unload (destroy) the area and load (instantiate) a new one, you don’t have to deal with trying to preserve some objects in the scene (and references to them) since they never get destroyed.

Plus you get some other potential benefits, like for example maybe you want to be able to look at a few different areas at the same time to get the big picture - you can just drop multiple area prefabs into the scene.

I think a title screen is a good example of when a separate scene makes sense - (where you would ‘continue’, ‘load’, or ‘start new game’). There’s nothing to persist between gameplay and the title screen. It’s understood even your game state might be lost (if not saved) when you exit the game to the title screen.

Privacy & Terms