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 name and score between different levels (scenes). Also, we will give the player the option of erasing his data.
We have included a complete project named BlueSpheres
in the 0423_07_04
folder. In order to follow this recipe, make a copy of that folder as your starting point.
To save and load player data, follow these steps:
BlueSpheres
project and make yourself familiar with the game by playing it a few times and examining the contents of the scenes. The game starts on the menu_start
scene, inside the Scenes
folder.Player
C# script with the following code:// file: Player.cs using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public static string username = null; public static int score = -1; public static void DeleteAll(){ username = null; score = -1; } }
MenuStart
C# script with the following code:// file: MenuStart.cs using UnityEngine; using System.Collections; public class MenuStart : MonoBehaviour { const string DEFAULT_PLAYER_NAME = "PLAYER_NAME"; private string playerNameField = DEFAULT_PLAYER_NAME; private void OnGUI() { string rules = "Easiest Game Ever -- Click the blue spheres to advance."; GUILayout.Label(rules); if(Player.username != null) WelcomeGUI(); else CreatePlayerGUI(); } private void WelcomeGUI() { string welcomeMessage = "Welcome, " + Player.username + ". You currently have " + Player.score + " points."; GUILayout.Label(welcomeMessage); bool playButtonClicked = GUILayout.Button("Play"); bool eraseButtonClicked = GUILayout.Button("Erase Data"); if( playButtonClicked ) Application.LoadLevel(1); if( eraseButtonClicked ) ResetGameData(); } private void ResetGameData() { Player.DeleteAll(); playerNameField = DEFAULT_PLAYER_NAME; } private void CreatePlayerGUI() { string createMessage = "Please, insert your username below and click on Create User"; GUILayout.Label(createMessage); playerNameField = GUILayout.TextField(playerNameField, 25); bool createUserButtonClicked= GUILayout.Button("Create User"); if( createUserButtonClicked ){ Player.username = playerNameField; Player.score = 0; } } }
SphereClick
C# script with the following code:// file: SphereClick.cs using UnityEngine; using System.Collections; public class SphereClick : MonoBehaviour { private void OnGUI(){ GUILayout.Label( "Score: " + Player.score ); } private void OnMouseDown() { if( gameObject.CompareTag("blue") ) Player.score += 50; Destroy(gameObject); GotoNextLevel(); } private void GotoNextLevel() { int level = Application.loadedLevel + 1; Application.LoadLevel(level); } }
OnGUI()
method of the MenuEnd
C# script with the following code:private void OnGUI() { GUILayout.Label("Congratulations " + Player.username); GUILayout.Label("You have finished the game, score = " + Player.score); bool mainMenuButtonClicked = GUILayout.Button("Main Menu"); if( mainMenuButtonClicked ) Application.LoadLevel(0); }
The Player
class uses static (class) properties username
and score
to store the current player's name and score. Since these are public static properties, any object from any scene can access these values (static properties are "remembered" from scene to scene). This class also provides the DeleteAll()
public static method that resets username
to null
and score
to -1
.
The MenuStart
class tests the value of Player.username
. If it is null
, then a textbox and button are provided to allow the user to enter a new username; which is then stored in Player.username
. Once the value in Player.username
is no longer null
, the user is offered a button to start playing the game, or to erase the username and any score currently stored in Player
class.
The three game-playing scenes now include a GUI
method to display the current value in the Player.score
property, which has 50
added to it each time a sphere tagged blue
is clicked.
The MenuEnd
class congratulates the player by name (retrieving the name from Player.username
), and tells them their score (from Player.score
).
18.225.234.24