- Home /
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: http://answers.unity3d.com/questions/20773/how-do-i-make-a-highscores-board.html
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.
The script is not compiling, but I've copied as much as I can from the above solution. Is there something I'm doing wrong?
"It doesn't compile", well how about you post the compile error then? Or even try and read/understand it yourself!
Answer by mattmanj17 · Oct 18, 2013 at 02:00 AM
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!
glad I could help! if you like my answer, select it as the "right" answer, so your question can be marked as resolved.