- Home /
Score per scene
I need to keep track of the score for each level so I can compare it against a replay of that level ( a highscore thing ).
I have a script attached to a game object with a points
property.
public static int points;
And I increment this when I need to add more points.
I also have a highscore
property which is updated if the score if higher than the stored highscore.
if (highScore == 0 || score > highScore) {
highScore = score;
}
The problem is that when you go to a new level and post a new highscore then come back to this level the highscore property is the score you got on the other level and not the last score you got on this level.
I hope that makes sense.
Answer by JayOhhh · May 30, 2014 at 04:55 PM
I believe you are going to want to use a player preference. Set the score in a player preference and load it when you need to.
http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Example:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
//set level 1 score
void Example() {
PlayerPrefs.SetInt("Level1Score", 10);
}
// set level 2 score
void Example() {
PlayerPrefs.SetInt("Level2Score", 10);
}
// get level 1 score
void Example() {
print(PlayerPrefs.GetInt("Level1Score"));
}
// get level 2 score
void Example() {
print(PlayerPrefs.GetInt("Level2Score"));
}
}
Answer by sarusian · May 30, 2014 at 05:04 PM
It seems like there is only one variable, a better better be something that deals with each individual level, I would create an example, but I'm afraid that it wouldn't be debug at the moment, so the best path is something if its level1 then update highscore1 else if its level 2 update highscore2 also, makesure you have a script that is saving this data, otherwise one you load a diffrent level it just dissapears.