Question by
farvind · Jan 11, 2017 at 03:43 PM ·
uiscripting beginnerplayerprefsscore system
Saving highscore for each levels
I am having trouble with loading highscore using playerprefs.
This is what my highscore script looks like:
// Use this for initialization
void Start()
{
CurrentLevel = LevelLoader.FindObjectOfType<LevelLoader>().CurrentLevel;
highscore = PlayerPrefs.GetInt("Level" + CurrentLevel.ToString() + "_score", highscore);
text = GetComponent<Text>();
Score = 0;
}
// Update is called once per frame
void Update()
{
if (Score < 0)
{
Score = 0;
}
if (Score >highscore)
{
highscore = Score;
PlayerPrefs.SetInt("Level" + CurrentLevel.ToString() + "_score",highscore);
}
text.text = "" + highscore;
}
public static void AddPoints(int pointsToAdd)
{
Score += pointsToAdd;
}
and this is the script i useed to get the highscore so i could use the score for 3-star rating system:
//Use this for initialization
void Start ()
{
high= PlayerPrefs.GetInt("Level" + CurrentLevel.ToString() + "_score", highScore.highscore);
CheckCurrentLevel();
PlayerInZone = false;
}
// Update is called once per frame
void Update () {
if (PlayerInZone)
{
AudioSource.PlayClipAtPoint(WinMenu, transform.position);
PlayerPrefs.Save();
Playerwin.SetActive(true);
TimeManager.FindObjectOfType<TimeManager>().countingTime =100;
SaveMyGame();
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
PlayerInZone = true;
Debug.Log("GameEnd");
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player")
{
PlayerInZone = false;
}
}
public void CheckCurrentLevel() {
for (int i = 0; i < LevelAmount; i++)
{
if (Application.loadedLevelName == "Level" + i)
{
CurrentLevel = i;
// PlayerPrefs.GetInt("Level" + CurrentLevel.ToString() + "_score", highscore);
}
}
}
void SaveMyGame()
{
int NextLevel = CurrentLevel + 1;
if (NextLevel < LevelAmount)
{
PlayerPrefs.SetInt("Level" + NextLevel.ToString(), 1);
PlayerPrefs.SetInt("Level" + CurrentLevel.ToString() + "_score", highScore.highscore);
}
else
{
PlayerPrefs.SetInt("Level" + CurrentLevel.ToString() + "_score", highScore.highscore);
}
}
I wish i could solve this because i don't know where the error lies
Comment
Your answer
Follow this Question
Related Questions
Showing a high score in the menu 1 Answer
How can I keep a score counter of how many points the player has collected on the screen? 1 Answer
Display PlayerPref in InputField 1 Answer
How does one reference a UI instance from a prefab? 1 Answer
In the next scene show the score using player prefab before next level 1 Answer