- Home /
 
Value sometimes isn't updated.
I have a timer that counts minutes, seconds and milliseconds survived. After death the player's current, previous and best time are shown. As they press space they start a new run, and their former current time should be updated into their next previous time. However, for some reason, sometimes the previous time gets updated into a blank, and sometimes it updates correctly. This worked correctly previously, when I had this working through a binaryformatter, but as I changed it into working through playerprefs, it doesn't operate reliably anymore.
 public class WebTimer : MonoBehaviour
 {
 
     public Text timerText;
     public Text currentText;
     public Text previousText;
     public Text bestText;
 
     public GameObject current;
     public GameObject previous;
     public GameObject best;
 
 
     private float milliseconds = 0;
     private float seconds = 0;
     private float minutes = 0;
 
     private float preMilliseconds = 0;
     private float preSeconds = 0;
     private float preMinutes = 0;
 
     private float bestMilliseconds = 0;
     private float bestSeconds = 0;
     private float bestMinutes = 0;
 
     private GameOver death;
 
 
     public void Awake()
     {
 
             preMinutes = PlayerPrefs.GetFloat("preMinutes");
             preSeconds = PlayerPrefs.GetFloat("preSeconds");
         preMilliseconds = PlayerPrefs.GetFloat("preMilliseconds");
 
         bestMinutes = PlayerPrefs.GetFloat("bestMinutes");
         bestSeconds = PlayerPrefs.GetFloat("bestSeconds");
         bestMilliseconds = PlayerPrefs.GetFloat("bestMilliseconds");
 
     }
 
     private void Update()
     {
         if (!GameObject.Find("BorderCollider").GetComponent<GameOver>().gameOver)
         {
             minutes = (int)(Time.timeSinceLevelLoad / 60f);
             seconds = (int)(Time.timeSinceLevelLoad % 60f) % 60;
             milliseconds = (int)(Time.timeSinceLevelLoad * 1000f) % 1000;
 
             timerText.text = "Time:  " + minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + milliseconds.ToString("00");
 
         }
 
 
         if (GameObject.Find("BorderCollider").GetComponent<GameOver>().gameOver)
         {
 
             if (minutes > bestMinutes)
             {
                 bestMilliseconds = milliseconds;
                 bestSeconds = seconds;
                 bestMinutes = minutes;
             }
             else if (minutes == bestMinutes && seconds > bestSeconds)
             {
                 bestMilliseconds = milliseconds;
                 bestSeconds = seconds;
                 bestMinutes = minutes;
             }
             else if (seconds == bestSeconds && milliseconds > bestMilliseconds)
             {
                 bestMilliseconds = milliseconds;
                 bestSeconds = seconds;
                 bestMinutes = minutes;
             }
 
 
             if (Input.GetButtonDown("Jump")) //this is the part where the updating of values should happen, but it only occurs sometimes
             {
                 preMilliseconds = milliseconds;
                 preSeconds = seconds;
                 preMinutes = minutes;
 
 
 
                 if (GameObject.Find("BorderCollider").GetComponent<GameOver>().gameOver && Input.GetButtonDown("Jump"))
                 {
                     SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
 
                     current.SetActive(false);
                     previous.SetActive(false);
                     best.SetActive(false);
                 }
             }
 
             current.SetActive(true);
             previous.SetActive(true);
             best.SetActive(true);
 
             currentText.text = "Time Survived: " + minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + milliseconds.ToString("00");
             previousText.text = "Previous Time: " + preMinutes.ToString("00") + ":" + preSeconds.ToString("00") + ":" + preMilliseconds.ToString("00");
             bestText.text = "Best Time:      " + bestMinutes.ToString("00") + ":" + bestSeconds.ToString("00") + ":" + bestMilliseconds.ToString("00");
             Save();
 
             if (Input.GetKeyDown("escape"))
             {
 
                 preMilliseconds = milliseconds;
                 preSeconds = seconds;
                 preMinutes = minutes;
                 Application.Quit();
             }
         }
     }
 
 
     public void Save()
     {
 
         PlayerPrefs.SetFloat("preMinutes", preMinutes);
         PlayerPrefs.SetFloat("preSeconds", preSeconds);
         PlayerPrefs.SetFloat("preMilliseconds", preMilliseconds);
 
         PlayerPrefs.SetFloat("bestMinutes", bestMinutes);
         PlayerPrefs.SetFloat("bestSeconds", bestSeconds);
         PlayerPrefs.SetFloat("bestMilliseconds", bestMilliseconds);
     }
 
 
 }
 
              Something to add: Tried with a Debug.Log, had it printing the preSeconds into the log, and even if the text in-game was left blank, the log still contained the correct value.
Answer by Paliandro · Mar 01, 2021 at 07:13 PM
I'm dumb, the answer was that my text object wasn't wide enough and some combinations of characters caused it to dissapear.
Your answer
 
             Follow this Question
Related Questions
Restart Mecanim's Animator controller 3 Answers
is resarting unity to update scripts a bug or an error on my part? 0 Answers
how do i make this to a negative (ShopRespawn < 180)? 1 Answer
Restart a function 1 Answer
How to restart/rerun a script? 2 Answers