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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

High score value remains constant in game over scene, even if player has scored higher.

Discussion in 'Scripting' started by phantom_x404, Apr 28, 2020.

  1. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    I am trying to set up a high score system for an infinite runner game, but the text always remains stuck at 21, Below is the code and further down you'll find the explanation for what I am trying to do here.

    CODE:
    For setting High Score and normal Score:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. public class Score : MonoBehaviour
    4. {
    5.     public Transform player;
    6.     public Text ScoreValue; //means, input is required(the text that needs to be constantly updated i.e. score), since a variable has been created
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         int scoreValInt = Mathf.RoundToInt((player.position.z - 18) / 20);
    11.         ScoreValue.text = ((player.position.z-18)/20).ToString("0"); //to string 0 to remove all decimal points
    12.         PlayerPrefs.SetString("CurrentScore", (scoreValInt.ToString("0")));
    13.         PlayerPrefs.SetInt("IntHighScore", scoreValInt);
    14.         Debug.Log(PlayerPrefs.GetInt("IntHighScore"));
    15.         if (scoreValInt > PlayerPrefs.GetInt("IntHighScore"))
    16.         {
    17.             PlayerPrefs.SetString("HighScore", (scoreValInt.ToString("0")));
    18.         }
    19.        
    20.     }
    21. }


    For Displaying High Score on Game Over scene:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. public class HighScore : MonoBehaviour
    4. {
    5.     public Text ValueToUpdate;
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         ValueToUpdate.text = PlayerPrefs.GetString("HighScore", "0").ToString();
    10.     }
    11. }
    12.  

    I have two scenes, i) Game Scene and ii) Game Over Scene
    When the game ends the scene transitions to Game Over scene using Scene Management(done in another script). The main score gets updated every time the game ends, but for the High Score, even if the score is higher than the previously set high score, the High Score in Game Over scene does not get updated and for some reason it is stuck at 21, can someone give me a solution for this? It'd be very much appreciated :)
     
  2. kaarloew

    kaarloew

    Joined:
    Nov 1, 2018
    Posts:
    360
    Common tip, learn to use debugger. Set breakpoint to your code, and run it once.

    Code (CSharp):
    1. PlayerPrefs.SetInt("IntHighScore", scoreValInt);
    2. Debug.Log(PlayerPrefs.GetInt("IntHighScore"));
    3. if (scoreValInt > PlayerPrefs.GetInt("IntHighScore"))
    On first line you are settings scoreValInt to IntHighScore. So condition on third line cannot be true, because those value will always be equal
     
  3. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26

    I have set it equal because for the value of Highscore is supposed to be ScoreValInt, but only when ScoreValInt is greater than HighScore.
    I am pretty confused about this rn, what changes do you suggest I make in the code ?