Hi all, first post here so hello everybody. I’m new to Unity.
Here is my first question and problem
I have a simple score and high score script implemented on my endless jumper game.
After I die the game shows a Game Over panel with you score and high score. But if you make a new high score it will firstly show a “new high score” panel before entering the regular Game Over panel. But at the moment the “new high score” panel also gets shown when you High Score is the same as the latest High Score. I can’t seem to figure out what I did wrong.
This is partly the code for handling the score and panels:
First Function in ScoreManager:
public void IncrementScore () {
score++;
scoretext.text = "" + score;
}
public void HighScore ()
{
if (score > highScore) {
highScore = score;
PlayerPrefs.SetInt (highScoreKey, highScore);
PlayerPrefs.Save ();
}
highScorePreText.text = "New High";
highScoreText.text = "" + highScore;
}
public int GetHighScore ()
{
return this.highScore;
}
public int GetScore ()
{
return this.score;
}
}
And the panel hiding etc in another script GameOverManager:
// shows the Game Over Panels. Also shows the High Score Panel if a High Score is made
public void GameOverShowpanel ()
{
if (ScoreManager.instance.GetHighScore () > ScoreManager.instance.GetScore ()) {
scoreText.SetActive (false);
gameOverpanel.SetActive (true);
finalScore.text = "Score: " + "" + ScoreManager.instance.GetScore ();
finalHighScore.text = "High Score: " + "" + ScoreManager.instance.GetHighScore ();
gameOverAnim.Play ("FadeIn");
// Shows the High Score Panel if a High Score is made
} else {
scoreText.SetActive (false);
hScorePreText.SetActive (true);
hScoreText.SetActive (true);
panelNewHigh.SetActive (true);
}
}
Because your saying if Highscore is strictly greater than score - Do the no Highscore stuff
Else (score is greater then OR EQUAL TO highscore) – Do new highscore stuff
Just change the original if to this:
if (ScoreManager.instance.GetHighScore () >= ScoreManager.instance.GetScore ()) {
Now they have to actually beat the high score not just tie it.
I tried that, but the weird thing is if I change it to >= the “New High Score” panel won’t popup at all. Even not after making a new high score. I can’t seem to find the problem why the “>=” isn’t working.
Try adding some debugs in the sections printing out what the game thinks highscore and regular score are:
Debug.Log(" HighScore is: " + ScoreManager.instance.GetHighScore() + ", Score was : " + ScoreManager.instantce.GetScore());
Stick that code in both sections of the if / else and see whats going on Maybe you think there was a high score but the game didn’t because of some bug in the scoring
Stupid me, my code for showing a high score panel before the regular panel when making a new high score would never work because your score at that moment is always as high as the high score of that moment. So “greater then” > would never work?
if (score > highScore) {
highScore = score;
So highscore is always equal to score, so showing a panel based on the high score comparing to score won’t work?
Have ScoreManager have a new variable called prevHighScore. Everytime you get a new HighScore your code stays the same Then your GameOverCode looks like this:
public void GameOverShowpanel ()
{
if (ScoreManager.instance.GetPrevHighScore () >= ScoreManager.instance.GetScore ()) {
scoreText.SetActive (false);
gameOverpanel.SetActive (true);
finalScore.text = "Score: " + "" + ScoreManager.instance.GetScore ();
finalHighScore.text = "High Score: " + "" + ScoreManager.instance.GetHighScore ();
gameOverAnim.Play ("FadeIn");
// Shows the High Score Panel if a High Score is made
} else {
scoreText.SetActive (false);
hScorePreText.SetActive (true);
hScoreText.SetActive (true);
panelNewHigh.SetActive (true);
ScoreManager.instance.UpdatePrev();
}
Obviously GetPrevHighScore just gets the value in prevHighScore, and the new method UpdatePrev copies the current HighScore into previous for the next game.
So the logic would be this:
HScore =0
PrevHScore = 0
Game 1:
score = 10
ScoreManger puts 10 into HScore
GameOverCode:
PrevHscore(0) >= 10 false
Does new Hishscore stuff
UpdatePrev puts Hscore (10) into PrevHScore so now its 10
Game 2:
score = 10
No new HScore so GameOver
PrevHScore(10) >= 10 true
normal Score Screen
Game 3:
score = 20
PrevHScore(10) >= 20 false
Does new HighScore stuff
UpdatePrev puts HScore(20) into PrevHScore so its now 20
There is no if statement in UpdatePrev it should always happen. Its just a temporary variable really to remember what the high score was before we updated it. So our GamOverCheck can also check if the current score beat the old score. And it doesn’t need to be saved in PlayerPrefs, its just something we are using during runtime
public void UpdatePrev ()
{
prevHighScore = highScore;
}
prevHighScore should just be initialized to current HighScore when the game loads up. Then it will just be a variable to hold what the highscore was last game. After Your GameOverScreen happens we update it with the current HighScore.
I removed the if statement from the UpDatePrev() but still got the highscore wrong. For example I score 3 as highscore and score a 3 again, then still the high score panel pops up.
But doesn’t the prevHighScore needs to know what the previous highscore was?
It does know what the previous highscore was. It starts of being the current Highscore. Lets say Highscore is = 10; PrevHighScore is also = 10. Now:
Player plays a game and scores 20
You call ScoreManager.HighScore()
It sets HighScore = 20
PrevHighScore is still 10 (thus its the previous high score)
You Call GameOverShowPanel
ScoreManger.instance.GetPrevHighScore() >= ScoreManger.instance.GetScore() should be 10>=20 false
So it should do the else and put up the New HighScore screen
Also Calls UpdatePrev So now PrevHighScore = Highscore = 20
Next Game:
Player plays a game and scores 20
ScoreManager.HighScore() doesnt’ change anything
GameOverShowPanel gets called
ScoreManger.instance.GetPrevHighScore() >= ScoreManger.instance.GetScore() should be 20>=20 true
So it calls the “normal” end of game scoring section
Note we don’t need to call UpdatePrev here though we could. If we did it would just copy HighScore(20) into PrevHighScore which is still 20
Try adding Debug Logs back into that section except this time print out PrevHighScore and score. Maybe you setting PrevHighScore somewhere you shouldn’t be. On GameLaunch it should load the HighScore out of PlayerPrefs. And then only get Updated in UpdatePrev
I know what you are saying but the prevHighScore stays empty. When I SetInt to prevHighScore when called at UpdatePrev() and then on the next game load the PlayerPreft.GetInt(prevHighScoreKey, prevHighScore) in Awake, the debug tells me the prevHighScore is still empty.
Sorry, I’m still new to coding and Unity, trying hard
You set prevHighScore and Highscore to the same value when the game starts. You don’t care what prevHighScore was when the game ends so we don’t need to save it
Got it working , and I get it now.
Thank you so much for helping me out! It was indeed the prevHighScore that was getting the wrong information form the PlayerPref.