- Home /
PlayerPref set int and get int
Sorry to have to ask, but im relitively new to unity and, though I have done a lot of research, I simply can't seem to find what im doing wrong here, each time I play the game or reset the level, the highscore is reset to 0 with the score. Heres my code:
using UnityEngine;
using System.Collections;
public class Score2 : MonoBehaviour {
public int score=0;
public int highScore;
// Update is called once per frame
void FixedUpdate () {
score++;
if (score > highScore){
highScore = score;
PlayerPrefs.SetInt("highScore", highScore);
PlayerPrefs.Save ();
}
guiText.text = "Score: " + score + "\nHighScore: " + highScore;
}
void Start(){
score = 0;
PlayerPrefs.GetInt("highScore");
}
}
Answer by Graham-Dunnett · May 21, 2015 at 09:15 AM
Look at line 19. You ask for the high score from the player prefs, but you don't actually save it anywhere. Don't you want to assign this to the highscore
variable? Also, on the very first time the game runs, there will not be a highscore value written into the player prefs, and so the line 19 will probably return an error. You will want to check for this scenario and set the highscore to zero.
Thank you very much for the answer, thanks to your tips I managed to figure it out fairly quickly! You sir get an upvote and a huge thanks.
Your answer
Follow this Question
Related Questions
How to save a high score 2 Answers
Sorting Error 0 Answers
How can i get the following script back on track to being a single level high score saver 0 Answers