Tutorial: Persistent Score - CodingDino/Dino-Unity-Toolkit GitHub Wiki

In this tutorial, you will learn how to save the player's score when leaving one scene, and load it again when entering a new scene. This can be used to make the score persist between areas or levels. We'll accomplish this by using the saveID and the Save/Load functions on the score's Data component.

This tutorial requires a score system such as the one from Tutorial: Score. It also requires some kind of player movement (like from the Tutorial: Platformer Player Movement) and a way to trigger a scene change, such as the doors in Tutorial: Doors.

Before we start, let's clearly state our goal behaviour:

Save our score when the player leaves one area, and load the score when the player enters the next scene.

This can be broken down into two separate activator / action pairs.

Action 1 - Save Score

Let's start in the first scene, which our player will shortly be exiting, and where the score we want to save is located. In the stated goal behaviour above, the first part of the sentence describes an Action that should occur:

Save our score...

The action to save the score is part of the IntData component that we use to track our score. So, we actually don't need to add any new components to make this action work!

What we DO need to do is set the Save ID property on our IntData component. This tells Unity internally how to keep track of our saved data. Let's choose an easy to remember save ID: "Score".

Select your score's GameObject and find the IntData component on it. Find the Save ID property and type "Score" into the box.

Activator 1 - Exit First Scene

In the first half of our double goal behaviour, our Activator is:

...when the player leaves one area...

If you've used the Tutorial: Doors to create a portal between scenes, you already have an activator that starts the SceneAction loading the next scene. We just need to call the ActionSaveValue() on our IntData to trigger the new Action.

Select your door, and move to the OnCollision2DActivator. In the On Collision Enter () section, add a new Action to the list and link the Score GameObject. Find the IntData in the drop down and choose the ActionSaveValue() function.

Your score value should now save when you touch the door! Let's get it loading in the next scene.

Action 2 - Load Score

Now you need to open your second scene, where the player will enter, and where you want your score to transfer. In the stated goal behaviour above, our second Action is described in the second half of the sentence:

...load the score...

As with saving, this is already part of the IntData component on the score reader. So again, we don't need to add any new components - we just need to set up the save ID just as in the first section above! Set it to the same ID as in the previous scene: "Score".

Select your score's GameObject and find the IntData component on it. Find the Save ID property and type "Score" into the box. This is exactly as in the first step above, see the video there if you need more guidance.

WARNING: It is REALLY important to mind your spelling and capitalisation for the Save ID! If it's not the same in both scenes, the score you save won't be the same score you load up later!

Activator 2 - Enter the Second Scene

In the second half of our double goal behaviour, our Activator is:

...when the player enters the next scene...

For this, we can use a InitializerActivator. This Activator can be used to trigger Actions when the scene starts.

In the second scene, select the score's GameObject. Click "AddComponent" and choose "Dino Toolkit > Activator > InitializerActivator" from the list.

In the "On Awake" Activation List, add a new Activation. Drag in the score GameObject and choose "IntData > ActionLoadValue()" from the dropdown.

This will load whatever data has been saved to the linked Save ID, up into our score's active IntData. This should automatically call any Actions in the "On Value Change ()" Activation List, such as Actions to update the text displaying the score.

Try it out - it should end up something like this:

You're score is now all set up! If you want to, you can also use the ActionClearSavedValue() on the IntData to remove any saved data, for example if you want to reset the player's score. If you want to go one step further and add a high score system, see Tutorial: High Score.