Reputation: 111
I have created a simple game where the player collects points on each level, and once the level is completed it will show the total high score. I have recently learned how to save using player prefs and I am able to store the high score of the player in each specific level.
this is the pic when the player completes a level:
And this is the code for it:
void Update()
{
int tScore = PlayerPrefs.GetInt(SceneManager.GetActiveScene().name, 0);//it is adding the points with the highscore again for some reason
scoreDisplay.text = "TOTAL POINTS: "+ PlayerController.count.ToString();//display score
timeDisplay.text = "TIME LEFT: " + FindObjectOfType<TimeController>().timeText.text;//display remainder time
scorePoints = PlayerController.count;
if (FindObjectOfType<TimeController>().minutes != 0)
{
timePoints = (FindObjectOfType<TimeController>().sec + 60)/2;
}
else
{
timePoints = FindObjectOfType<TimeController>().sec/2;
}
highScore = timePoints * scorePoints;
highScores.text = "HIGH SCORE: " + highScore;
if (tScore <= highScore)
{
PlayerPrefs.SetInt(SceneManager.GetActiveScene().name, highScore);
}
}
I am saving the entire scene so that the high score for the specific level is saved. The first thing I do is to get the previous high score that the player has scored previously from that same level using the 'GetInt' as tScore or if there is none the default is zero. The high score is then calculated simply by taking the scorePoints (which I get from another class 'PlayerController.count' in the code) and multiply it with the remainder of the time which you can see in the code above. I then compare the highScore with the previous highscore of the same level (tScore) and if it is higher it will override as the new highScore for that level.
This code works most of the time but for some reason, sometimes, the highScore for that scene which is already calculated, adds again with the points (example with the pic above, the high score is 368, but when I use the GetInt, it adds again with the points meaning (368+16 = 384) becoming the high score (overwriting the previous high score) which is wrong. Can someone tell me what is wrong with this?
Upvotes: 0
Views: 206
Reputation: 809
OK, I will try to give an answer. Please note that I may have misunderstood what you wanted. Anyway, I would create a simple MonoBehaviour called for example "ShowScore.cs" that can be placed anywhere in the scene (so why not on the canvas).
using UnityEngine;
public class ShowScore : MonoBehaviour
{
public UI.Text scoreDisplay;
public UI.Text timeDisplay;
public UI.Text highScores;
public void GameOver()
{
string sceneName = SceneManager.GetActiveScene().name;
int currentHighScore = PlayerPrefs.GetInt(sceneName, 0);
TimeController timeController = FindObjectOfType<TimeController>();
int scorePoints = PlayerController.count;
int seconds = timeController.sec + timeController.minutes * 60;
Debug.Log("timeController.sec: " + timeController.sec.ToString());
Debug.Log("timeController.timeText.text: " + timeController.timeText.text);
int timePoints = (int)(seconds/2f);
int score = timePoints * scorePoints;
scoreDisplay.text = "TOTAL POINTS: "+ scorePoints.ToString();
timeDisplay.text = "TIME LEFT: " + seconds.ToString();
if (score > currentHighScore)
{
PlayerPrefs.SetInt(sceneName, score);
highScores.text = "HIGH SCORE: " + score.ToString();
}
else
{
highScores.text = "SCORE: " + score.ToString() + " (the HIGH SCORE is " + currentHighScore.ToString() + ")";
}
}
}
I would then call the function GameOver() once the end trigger has fired. Good luck!
Upvotes: 1