Hello everyone.
Im currently learning how to use PlayerPrefs. i understood pretty much how it works, but when i save the score for example, (score : 5), in the level 2 it shows (score : 5) but as soon i get one more point, it overwrites on the 5, the number 6. and for the whole level 2, i have written the 5, and over it the new numbers are appearing.
Same with lives. Any idea how to make number 5 move to 6? Instead of just staying on the screen?
im guessing it has to do with:
If you don’t want the player to retain the score from level one, then you should set a different key to the PlayerPrefs method in level two. Like so:
Level 1:
PlayerPrefs.SetInt("Level1Score",score);
Level 2
PlayerPrefs.SetInt("Level2Score",score);
Or, if you want the player to retain his score after level completion, it should be like so:
Level 1:
static var score : int = 0;
PlayerPrefs.SetInt("Score",score);
Level 2:
var HighScore : int = PlayerPrefs.GetInt("Score");
static var Score : int = Level1Script.score; //Where Level1Script should be replaced with the actual script name.
function Update(){
if(Score > HighScore){
PlayerPrefs.SetInt("Score",Score);
}
}
Also, you should try to stay away from using static variables when you don’t have to. Make a reference to the script in question instead.
Furthermore, when dealing with multiple levels, it is usually always best to implement a GameManager. This would probably simplify your problem by quite a bit. Just google “unity gamemanager” and have a read.