Best way to manage my levels

Hello !
I am thinking about how managing the levels of my game and I am having trouble finding a suitable method.

Simply, here iss how my game works:
At launch, the scene that manages my main menu is active. From this menu, the user can choose the level he wants to play.
Each level is in a different scene. And in each scene, there are GameObjects specific to the latter.
For example, the GameObject Player does not have the same properties depending on the level.
Here is the typical and simplified tree structure of a scene:
Scene 1:
Root
-LevelContainer
–Grid
–Items
-PlayerContainer
–Player
–Camera
–UI

Of course, I can’t leave these different GameObjects as active because otherwise they would conflict with each other.
It is also imperative that I can reset the GameObjects to the state they were when the game was launched.

So I was thinking of doing something like this:

  1. When launching the game (landing on the main menu), we make Items and PlayerContainer inactive to avoid any conflict.
  2. When a level is loaded, the corresponding GameObjects Items and PlayerContainer are set to active.
  3. If we want to restart the level or join the main menu, we load the corresponding scene while deactivating Items and PlayerContainer of the previous scene.

However I can’t find a good method to keep in memory references to Items and PlayerContainer, which are specific to each scene.
Do I have to create a script attached to each “Root” GameObject in which I put serializable variables ?
But in this case, how do I find these variables when I load a level ?

In code, it could look like this:

public GameObject PlayerContainer;
public GameObject Items;

Void Start() {
    PlayerContainer.SetActive(false);
    Items.SetActive(false);
}

Then in my script to load a level (which is attached to the parent GameObject of my buttons used by the user to make a choice for the level) :

public Player player;

public void StartGame() {
    //We load the scene
    PlayerContainer.SetActive(true); //How to get PlayerContainer and Items here ?
    Items.SetActive(true);

    player.Something();
}

Is it possible to do like this ? Or is there a better approach ?
It would be easier to make all the “Root” GameObjects inactive from the start and then activate them when loading a scene, but I don’t think that’s possible ?

Thank you very much in advance !

I am very ashamed to say that I had just to unload the scenes in the editor… I don’t know why I was looking for a much more complicated solution…
Sorry for the inconvenience.

I would like to note that you may find more success in using additive scene loading.

Generally having the player as their own scene additively loaded in alongside the level is a good way to keep things modular, and you don’t run at risk of forgetting to set up the player properly in any given scene.

Same goes for many other elements; camera, back end systems, etc.

1 Like

Hello and thank you for your tip.
I was indeed thinking of doing that for GameObjects that are not specific to a scene.

Spiney speaks with great wisdom. Here’s some more tidbits about that stuff:

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:

My typical Scene Loader:

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

Thank your very much for your help !