High score and persisting data

Pretty much every game has some sort of scoring system. You will now learn how to write simple code that calculates the score based on the distance the Player has traveled since the start of the level. We will then use this score value and store it in Unity PlayerPrefs to make sure that the value is remembered between sessions. PlayerPrefs is a very useful built-in Unity class that allows us to store and access data between Unity sessions.

Let's write the following method in the Player class:

High score and persisting data

We have finally come to a real-life example of a method that returns something. As you can see, the GetDistance() method returns a float value—the distance between the starting point and the current position of the player game object.

I won't go too much into the detai here. I encourage you to dive into the Unity Scripting Reference and search for Vector2.Distance to understand exactly how it works.

Having the GetDistance() method working, we can now call it from any place in the code and get the accurate distance traveled by the Player game object. The value returned by this method will be used directly as the player's score. Now is a good time to connect the score UI label directly to the GetDistance() method.

In the ViewInGame class, we declare the scoreLabel Text variable and add a new line to the Update() method just above the line where we are assigning the coin label.

High score and persisting data

Notice line 12. We are assigning the scoreLabel.text value by taking it directly from the float value returned from the PlayerController.instance.GetDistance() method. I hope this is not confusing! If it is, please remember what we said in the very early chapters of this book. A function can be the substitute for a value. In this case, when we are calling GetInstance(), we are getting back an int number straightaway. All we need to do is convert that float into a string. To do this, we use the ToString() function right away.

You are probably wondering, "What is the magical f0 parameter that we are passing in ToString("f0")?" It describes how we want our string to be formatted. In this case, we just want a whole number, without any decimals. I encourage you to read more online about C# ToString() formatting.

The last thing to do is connect the scoreLabel variable with the actual scoreLabel game object in the Hierarchy view. When you are ready, click on Play in Unity. The score label in the top-left corner will be showing the correct score value. Great job!

What you just learned is how to convert a float value to a string and display the value in the UI for the user.

We are getting close to finish writing the functionality for the inGameView. We will now work on persisting data. What does this mean? We want the user's high score to be remembered between game sessions. So, if the user closes the game and then opens it again, their high score will not be reset to zero. Rather, will remember their best score.

Unity does have an easy-to-use system for this. Let's jump into the scripting reference and search for PlayerPrefs.

High score and persisting data

The preceding screenshot has been taken from http://docs.unity3d.com/ScriptReference/PlayerPrefs.html. Let's take a look at the list of static functions again, specifically at the SetFloat and GetFloat functions:

  • SetFloat(string key, float value): This sets the value of the preference identified by the key.
  • public static float GetFloat(string key, float defaultValue = 0.0F): This returns the value corresponding to the key in the preference file, if it exists. If it doesn't exist, it will return defaultValue.

In simple words, we can ask Unity to save the float value accessible under the string key. Let's jump straight into our code. Open the PlayerController script and add the high-score-saving code.

High score and persisting data

What are we doing here? At the moment when the player is killed, we check whether the current distance traveled is greater than the float value stored under the high score key in Player prefs. If the distance is greater, it means the user has achieved a new high score, so Unity proceeds to line 79 and overrides the value in PlayerPrefs. Saving done!

Now, we need to make sure that highscoreLabel is displaying the correct value all the time. Yet again, add a new Text type variable, call it highscoreLabel this time, and add a line to the Update function.

Note

Make sure that all public member slots are connected to the correct game objects to avoid Null Reference Exceptions.

High score and persisting data

Our high score should work now. Play a game. Let the character die, restart the game, and see whether the value is displayed in the-top right corner.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset