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:
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...