Time for action – checking high scores in a new scene
The first thing we need is a way to store the high score whenever the level ends. This means we'll need to get the number of cannonballs fired at the end of a round and create a PlayerPrefs
entry for it. We'll do this by creating a static function to return the total number of cannonballs fired in the script and calling it from the target whenever it's hit. The steps to do so are as follows:
Create a new static function called
GetCannonballCount
in theScoreKeeper
class, and add a line that returns the integer value ofnumCannonballsFired
, as shown in the following code:public static int GetCannonballCount() { return numCannonballsFired; }
Open your
TargetScript
file, and add the following lines to itsOnCollisionEnter
function to save the player's score, which we'll access later from the high score script:void OnCollisionEnter(Collision collidingObj) { gameObject.renderer.material.color = Color.red; PlayerPrefs.SetInt("NewScore"...