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:
- When launching the game (landing on the main menu), we make Items and PlayerContainer inactive to avoid any conflict.
- When a level is loaded, the corresponding GameObjects Items and PlayerContainer are set to active.
- 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 !