Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Weird Hiccup with High Score Counter

Discussion in 'Scripting' started by Cheeko25, Jul 2, 2020.

  1. Cheeko25

    Cheeko25

    Joined:
    Jun 29, 2020
    Posts:
    8
    Hey there,
    I'm having an unusual issue with my High Score Counter.
    Clearing each obstacle nets 1 point. Everything works fine in terms of adding a point after each obstacle.
    The issue is that the high score works EXCEPT if you failed after you earn 1 point, in which it will reset the high score to "1".
    Clearing any other amount of obstacles saves fine (Ex. clearing 16 will save your high score as 16...if you clear 20 on your next run it saves 20 and so on) but if on any run you clear just 1 and fail, it saves 1 as the high score.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HighScore : MonoBehaviour
    7. {
    8.     public Text highScore;
    9.  
    10.     void Start()
    11.     {
    12.         highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.  
    18.         if (Score.score > PlayerPrefs.GetInt("HightScore", 0))
    19.         {
    20.             PlayerPrefs.SetInt("HighScore", Score.score);
    21.             highScore.text = Score.score.ToString();
    22.         }
    23.     }
    24. }
    25.  
    Does anyone have any idea where the problem could be? I've been looking at this for hours.
    Thank you!
     
  2. Elango

    Elango

    Joined:
    Jan 27, 2016
    Posts:
    107
    "HightScore"

    "HighScore"

    I suggest using static class with static/constant strings for you variables names. This way you will not make a typo like this.
     
    Last edited: Jul 2, 2020
    Cheeko25 likes this.
  3. Cheeko25

    Cheeko25

    Joined:
    Jun 29, 2020
    Posts:
    8
    Wow, I can't believe that's all it was! Thank you!! You're awesome!