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 ()
     {
           
     }
     //

}

Because you save score logic is in OnDisable().
You need to separate save score logic out from OnDisable and call them when GameOver.

Hello, if you could use Code sample tool , icon marked as “101010” in the tools section where you can find “Bold,Itallic…” i will try to help you :slight_smile: @samvilm