Retrieving the Coin Value

In order to retrieve the player’s coin value from PlayerPrefs, we need to make some adjustments to our PlayerStats script, as shown in Listing 13.3.

Listing 13.3 Changes to the PlayerStats Script

public class VictoryTrigger : MonoBehaviour{  void Start()  {    spriteRenderer = this.gameObject.GetComponent<SpriteRenderer>();    if(Application.loadedLevel != Constants.SCENE_LEVEL_1)    {      coinsCollected = PlayerPrefs.GetInt(Constants.PREF_COINS);    }    PlayerPrefs.SetInt(Constants.PREF_CURRENT_LEVEL,Application.loadedLevel);  }}

The Start() Function

The Start() function now tests whether or not the level being loaded is the first game level, and if it is, nothing new happens. If any ...

Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.