Problem with High Score System using PlayerPrefs [C#]

Hi,

I’m trying my first PlayerPrefs high score system. I need to save the best score and time so I can have them display on the results screen and the high score screen. I’m using the solution from the example below as my base script:

This is what I currently have:

using UnityEngine;
using System.Collections;

public class HighScoreS1 : MonoBehaviour {
	
		int newScore;
		int newTime;
		int oldScore;
		int oldTime;
	
	void AddScore(int score, int time){
		newScore = score;
		newTime = time;
		
		for(i = 0; i < 10; i++){
			if(PlayerPrefs.HasKey(i + "HScore")){
				if(PlayerPrefs.GetInt(i + "HScore") < newScore){
					//new score is higher than the stored score
					oldScore = PlayerPrefs.GetInt(i + "HScore");
					oldTime = PlayerPrefs.GetInt(i + "HScoreTime");
					PlayerPrefs.SetInt(i + "HScore",newScore);
					PlayerPrefs.SetInt(i + "HScoreTime",newTime);
					newScore = oldScore;
					newTime = oldTime;
				}
			} else{
				PlayerPrefs.SetInt(i + "HScore",newScore);
				PlayerPrefs.SetInt(i + "HScoreTime",newTime);
				newScore = 0;
				newTime = 0;
			}
		}
	}
}

Any help is appreciated. Thanks.

On line 15, when you begin your for loop, you forgot to write “int i=0” instead of just “i=0”. With out the “int”, i is never initialized, and the references to “i” in the for loop will not make sense. this leads to the error…

error CS0103: The name `i' does not exist in the current context

Just adding “int” in the for loop should fix it. The rest of the code seems fine as far as I can tell. Hope this helps, and good luck with your project!