Search Unity

Cant find out how to make a highscore

Discussion in 'Scripting' started by Ejim3, Oct 26, 2019.

  1. Ejim3

    Ejim3

    Joined:
    Oct 20, 2018
    Posts:
    4
    I can't find a way to fix this script every time I play the game the score goes up normally but the high score doesn't go up or change or do anything.

    My code:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class Score : MonoBehaviour
    7. {
    8.     public Text field;
    9.     public int currentDisplayScore = 0;
    10.     public Text highScore;
    11.  
    12.     void Start()
    13.     {
    14.         highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
    15.         StartCoroutine(CountUp());
    16.     }
    17.  
    18.     IEnumerator CountUp()
    19.     {
    20.  
    21.  
    22.         if (currentDisplayScore > PlayerPrefs.GetInt("HighScore", 0))
    23.         {
    24.             PlayerPrefs.SetInt("HighScore", currentDisplayScore);
    25.             highScore.text = currentDisplayScore.ToString();
    26.         }
    27.  
    28.         while (true)
    29.         {
    30.             currentDisplayScore++;
    31.             field.text = currentDisplayScore + "";
    32.             yield return new WaitForSeconds(1f);
    33.         }
    34.     }
    35.  
    36. }
    37.  
    38.  
     
  2. Seregone

    Seregone

    Joined:
    Nov 4, 2018
    Posts:
    13
    You are never updating your highscore in your "while(true)", just move your if statement in the while loop
     
    Ejim3 likes this.
  3. Ejim3

    Ejim3

    Joined:
    Oct 20, 2018
    Posts:
    4
    Thank you so much