- Home /
Highscores don't always get saved playerprefs unity3d
i want to save the highscores from each level idividually and write these in a different scene i also want to keep a total of all highscores from each level, this is working partially.
when i finish the first level the system seems to work fine, i got 5482, total says 5482 on the second level it also works and i got 2299, total says 7781, this is all correct when i finsh level 3 however i get 0 and the total stays on 7781, there is no difference in the way the level finishes and everything in the editor has been set up properly. the 4th level also gives 0, total remains the same, etc.
part of a bigger script but here's the code that writes score to text.
public int scorelevel;
public Text findText;
public bool highscore;
public bool totalhighscore;
void Start()
{
findText = GetComponent<Text>();
if(highscore){
if (PlayerPrefs.GetInt ("highscorelevel" + scorelevel) > 0) {
findText.text = "Highest score: " + PlayerPrefs.GetInt ("highscorelevel" + scorelevel);
} if(GameObject.Find ("LockedLevel" + (scorelevel)).active == true) {
findText.text = null;
}
}
if (totalhighscore) {
for (int j = 1; j < LockLevel.levels; j++){
PlayerPrefs.SetInt ("totalHighscore", PlayerPrefs.GetInt ("totalHighscore") + PlayerPrefs.GetInt ("highscorelevel"+j));
}
findText.text = "Total Score: " + PlayerPrefs.GetInt ("totalHighscore");
}
larger script but here's the part that saves the scores:
public class UnlockLevel : MonoBehaviour {
protected string currentLevel;
protected int levelIndex;
protected int levelIndex2;
void Start () {
//save the current level name
currentLevel = Application.loadedLevelName;
}
//check if the player hits the Finish
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Finish") {
Highscores ();
}
}
public void Highscores (){
for(int h = 1; h < LockLevel.levels; h++){
if(currentLevel == "Level"+h.ToString()){
//worldIndex = (i+1);
levelIndex2 = (h);
if(ScoreManager.score > PlayerPrefs.GetInt("highscorelevel"+levelIndex2.ToString())){
PlayerPrefs.SetInt("highscorelevel"+levelIndex2.ToString(), (int) ScoreManager.score);
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make move up text Animimation when Button click? 0 Answers
How To Crop the Gallery image for iphone in unity3d 0 Answers