Keeping track of the player's progress and user settings during a game is vital to give your game a greater feel of depth and content. In this recipe, we will learn how to make our game remember the player's score between the different levels (scenes).
We have included a complete project in a Unity package named game_HigherOrLower
in the 1362_10_04
folder. In order to follow this recipe, we will import this package as the starting point.
To save and load player data, follow these steps:
game_HigherOrLower
package.scene0_mainMenu
, then scene1_gamePlaying
, and so on).scene0_mainMenu
scene, inside the Scenes
folder.Player
with the following code:using UnityEngine; public class Player : MonoBehaviour { public static int scoreCorrect = 0; public static int scoreIncorrect = 0; }
scene0_mainMenu
scene, create a UI Text GameObject named Text – score, containing the placeholder text Score: 99 / 99.using UnityEngine; using System.Collections; using UnityEngine.UI; public class UpdateScoreText : MonoBehaviour { void Start(){ Text scoreText = GetComponent<Text>(); int totalAttempts = Player.scoreCorrect + Player.scoreIncorrect; string scoreMessage = "Score = "; scoreMessage += Player.scoreCorrect + " / " + totalAttempts; scoreText.text = scoreMessage; } }
scene2_gameWon
scene, attach the following C# script to the Main Camera:using UnityEngine; public class IncrementCorrectScore : MonoBehaviour { void Start () { Player.scoreCorrect++; } }
scene3_gameLost
scene, attach the following C# script to the Main Camera:using UnityEngine; public class IncrementIncorrectScore : MonoBehaviour { void Start () { Player.scoreIncorrect++; } }
The Player
class uses static (class) properties scoreCorrect
and scoreIncorrect
to store the current total number of correct and incorrect guesses. Since these are public static properties, any object from any scene can access (set or get) these values, since the static properties are remembered from scene to scene. This class also provides the public static method called ZeroTotals()
that resets both the values to zero.
When the
scene0_mainMenu
scene is loaded, all the GameObjects with scripts will have their Start()
methods executed. The UI Text GameObject called Text – score has an instance of the
UpdateScoreText
class as s script component, so that the scripts Start()
method will be executed, which retrieves the correct and incorrect totals from the Player
class, creates the scoreMessage
string about the current score, and updates the text property so that the user sees the current score.
When the game is running and the user guesses correctly (higher), then the scene2_gameWon
scene is loaded. So the Start()
method, of the IncrementCorrectScore
script component, of the Main Camera in this scene is executed, which adds 1
to the scoreCorrect
variable of the Player
class.
When the game is running and the user guesses wrongly (lower), then scene scene3_gameLost
is loaded. So the Start()
method, of the IncrementIncorrectScore
script component, of the Main Camera in this scene is executed, which adds 1
to the scoreIncorrect
variable of the Player
class.
The next time the user visits the main menu scene, the new values of the correct and incorrect totals will be read from the Player
class, and the UI Text on the screen will inform the user of their updated total score for the game.
There are some details that you don't want to miss.
Showing a score of zero out of zero isn't very professional. Let's add some logic so that the score is only displayed (a non-empty string) if the total number of attempts is greater than zero:
void Start(){ Text scoreText = GetComponent<Text>(); int totalAttempts = Player.scoreCorrect + Player.scoreIncorrect; // default is empty string string scoreMessage = ""; if( totalAttempts > 0){ scoreMessage = "Score = "; scoreMessage += Player.scoreCorrect + " / " + totalAttempts; } scoreText.text = scoreMessage; }
18.116.118.229