- Home /
whats wrong with my high score code?
when player dies it doesn't immediately update the high score. It only gets updated after dying the NEXT time (and shows the previously achieved high score). Weird.
I thought maybe because
//Get the highScore from player prefs if it is there, 0 otherwise.
highScore = PlayerPrefs.GetInt(highScoreKey,0);
is only called in start but i moved it to update and it made it worse. Here's my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameControll : MonoBehaviour
{
public static GameControll instance;
public GameObject gameOvertext;
public GameObject FlapToStartText;
public Text scoreText;
public Text highScoreText;
public bool gameOver = false;
public float scrollSpeed = -1.5f;
private int score = 0;
//
float waitTime = 0.8f;
bool doneWaiting = false;
//
//
public int highScore = 0;
string highScoreKey = "HighScore";
//
// Start is called before the first frame update
void Start()
{
//Get the highScore from player prefs if it is there, 0 otherwise.
highScore = PlayerPrefs.GetInt(highScoreKey,0);
FlapToStartText.SetActive (true);
//PlayerPrefs.DeleteAll();
}
//
void Awake ()
{
if (instance == null)
{
instance = this;
}
else if (instance !=this)
{
Destroy (gameObject);
}
}
// Update is called once per frame
void Update()
{
//
if(gameOver == true)
if(waitTime > 0)
{
waitTime -=Time.deltaTime;
}
else{
doneWaiting = true;
}
//if(gameOver == true && doneWaiting)
if (Input.GetButtonDown ("Jump"))
{
FlapToStartText.SetActive (false);
}
//
if (doneWaiting && gameOver == true && Input.GetButtonDown ("Jump"))
{
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
}
}
public void BirdScored ()
{
if (gameOver)
{
return;
}
score++;
scoreText.text = "Score: " + score.ToString ();
}
void OnDisable()
{
//If our scoree is greter than highscore, set new higscore and save.
if(score>highScore)
{
PlayerPrefs.SetInt(highScoreKey, score);
PlayerPrefs.Save();
}
}
public void Birddied()
{
gameOvertext.SetActive (true);
highScoreText.text = "HighScore: " + highScore.ToString();
gameOver = true;
}
//
void update ()
{
}
//
}
You have an "if" statement in your update method that goes nowhere...
My honest best guess, given the code that you've shown here is to double check the order in which your calling your methods, you need to be calling OnDisable() before Birddied() for the highscore to update... Why not just do both in the same method? If the bird is dead, then the highscore can't increase anyway... Right?
If I remove either of the 2 if statements then the game doesn't work the way I want it to. I tried rearranging the bird died and disable - to no effect. I'm really stuck here
I don't quite know what else could be happening, I would require a bit more of your code... I'm also confused as to what you're doing with:
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
What are you doing in the new scene you are loading?
You also said that it took 2 times before the new highscore appeared, is the highscore that appears the previous one or the highscore from the most recent one?
Answer by menstroke · Jan 11 at 09:24 AM
Because you save score logic is in OnDisable(). You need to separate save score logic out from OnDisable and call them when GameOver.
I've tried moving it from disable to update and to bird died but nothing changes.
After you save high score to player pref make sure you callhighScore = PlayerPrefs.GetInt(highScoreKey,score);
again to take the highScore into your text.
Your answer
Follow this Question
Related Questions
I'm trying to set a high score but I can't display it in another scene? 2 Answers
Can someone help me with my highscore script? 2 Answers
High score value remains constant in game over scene, even if player has scored higher. 1 Answer
Why isn't my high score sticking? 2 Answers
Multiple Cars not working 1 Answer