How do you save your game progress between scenes in Unity?
After finishing the integration of your saving system in Unity, the thing that you might notice is that it only works in one scene. You are curious how to make multiple levels and ensure that the saving system will save your player’s position, health, score and even the level scene in which the progress is saved. Just like the previous tutorial, this tutorial also uses JSON like in the Save Game in Unity video that was posted in April 2024.
If you want to make a level system for your game in Unity, you will need to open your level loader script. This C# script contains the if-statement that checks if you have a sufficient amount of coins. You also need to add a static int that will describe your current level. Here is a quick review of all scripts that you need to use:
Load Game Button
private void Start() { if (MenuNavigation.isLoaded) { Debug.Log("Level: " + LevelLoader.level); FindObjectOfType<PlayerDataManager>().LoadGame(); if (LevelLoader.level == 1) { SceneManager.LoadScene("LevelTwo"); } if (LevelLoader.level == 2) { SceneManager.LoadScene("Level3"); } } }
The load game button is just a simple script that checks when you press a button for loading the game. This script is used in all level scenes of the game, but on the main menu, you should use this script:
Menu Navigation
private PlayerData dataManager; public static bool isLoaded; public int levelToLoad; public void NewGame() { isLoaded = false; ScoreManager.scoreCount = 0; Debug.Log("Game is not loaded! " + isLoaded); SceneManager.LoadScene("SampleScene"); } public void LoadGame() { isLoaded = true; //ScoreManager.scoreCount = dataManager.score; Debug.Log("Game is loaded! " + isLoaded); Debug.Log("Level " + LevelLoader.level); if(LevelLoader.level == 0) { SceneManager.LoadScene("SampleScene"); } if (LevelLoader.level == 1) { SceneManager.LoadScene("LevelTwo"); } if (LevelLoader.level == 2) { SceneManager.LoadScene("Level3"); } }
Although the level loader script contains a static int, it is very important because it does not work without setting its value to static. That is because static values don’t change during the gameplay. Anyway, the level value will be connected to the Scene Manager. What does that mean? In the load level script, for the integer that defines your current level, you need to make sure that it exists in the void which contains a system of levels. If the value is 0, it means you are on the first level. case 0 will consist of only one line: scenemanager.loadscene(“SampleScene”). If the level value is 1, then it should load the second level, it will have SceneTwo inside the LoadScene() function.
Explanation
The level scene system is created by simply duplicating the SampleScene, with some small differences. You can also add multiple levels, but that will make the level system even more complex. Now it is time for the saving system. Go to player data and add public int level. Save the script and go to Player Data Manager. Make sure that you put the level value into the playerData. For introduction, you should use LevelLoader.level in order to make it work properly. Here are the slides that describe the implementation:
No responses yet