Multiple High Scores Issue...

So I was working on a Trivia game and I wanted the game to display high scores for each category. I ran into this issue and got really frustrated so I quit, and now I want to come back to it and see if I can get it worked out. I know I shouldn’t have given up in the first place but I find if I get frustrated I’ll end up creating more mistakes and issues!

So anyway. When I test the game, I’ll choose a category, I chose the “Games” category and just answered some questions to give myself a high score to display. Then when I returned to the category menu where I have all the high scores displayed next to each category, it was displaying the high score for the Games category as 1100 in my example… that was perfect. It was the high score I had got in my test anyway.

BUT… the rest of the categories were getting 550 for a high score. Half of what I just got for the Games category AND I had not even tested those categories at all.

I went through my code many, many times… Which added to A LOT of the frustration. I have declared each high score separately such as “sportshighscore” and “gameshighscore” and so on. I must have checked these things at least 10 times.

Does anyone have any idea what would cause this issue? Where it would be getting the half score from?

Please show your code!

Okay so I have multiple scripts for each category cause I couldn’t figure out how to have the questions loading from just one just one script, but that’s not really important for my issue cause what I did was working fine. Anyway! So here’s the script for the Games category high score:

So first of all, the code that gets, saves, loads, etc the high score:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GamesController : MonoBehaviour {

    public RoundData[] gamesRoundData;

    private HighScoreGamesManager gamesScore;

    // Use this for initialization
    void Start () {

        DontDestroyOnLoad (gameObject);

        SceneManager.LoadScene ("MenuScene");
        LoadGamesScore ();

    }

    public RoundData GetCurrentRoundData () {

        return gamesRoundData [0];

    }

    // Update is called once per frame
    void Update () {

    }

    public void NewGamesScore (int newGamesScore) {

        if (newGamesScore > gamesScore.gameshighscore) {

            gamesScore.gameshighscore = newGamesScore;
            SaveGamesScore ();

        }

    }

    private void LoadGamesScore () {

        gamesScore = new HighScoreGamesManager ();

        if (PlayerPrefs.HasKey ("gameshighscore"))
        {

            gamesScore.gameshighscore = PlayerPrefs.GetInt ("gameshighscore");

        }

    }

    public int GetGamesScore() {

        return gamesScore.gameshighscore;

    }

    private void SaveGamesScore () {

        PlayerPrefs.SetInt ("gameshighscore", gamesScore.gameshighscore);

    }
}

Then the storing the gameshighscore, which originally I had ALL of them, gameshighscore, sportshighscore, etc all in one script, but I separated them all to see if that would work, but it did nothing, anyway here’s the games one:

public class HighScoreGamesManager {

    public int gameshighscore;

}

And here’s for displaying the Games High Score (also separated all the categories with another attempt, didn’t fix anything) so it’s just the games category right now:

    public Text gamesHighScoreDisplayer;

    void Awake ()
    {
      
        gamesController = FindObjectOfType<GamesController> ();
        gamesHighScoreDisplayer.text = gamesController.GetGamesScore ().ToString ();

    }

And this part in the actual game code which sends the new score to be saved:

    public void EndRound () {

        isRoundActive = false;

        dataController.NewGamesScore (gamesPlayerScore);

        questionDisplay.SetActive (false);
        roundEndDisplay.SetActive (true);

    }

And all of my other codes are the exact same, the ONLY difference is replacing “games” with the appropriate category. I’ve gone over that way too many times and there’s definitely no errors with that part. For some reason, no matter what I’ve changed and tried, it continues to give all other categories half the high score of whatever one I play.

I’m sure there has to be some simple solution to this that I’m missing?

I use binary formatter to save all scores and highscores, this way you dont have any errors on trying to get scores populated into a scene… Plus the way you have it, seems a little over the top. I would make a simple game manager script and have your high score texts impelemted to it, as well as your scores… Then create a Script and call it Game and add this to it…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]




    public class Game
{

    public static Game current = new Game();

    public int _highScore1;
    public int _highScore2; // And so on
}

Then create another script and call it SaveLoad and add this

using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public static class SaveLoad
{

    public static void SaveGame()
    {


        BinaryFormatter bf = new BinaryFormatter();

        FileStream file = File.Create(Application.persistentDataPath + "/SaveGame.dat");
        Game game = new Game();


       
        game._highScore1 = Game.current._highScore1;
        game._highScore2 = Game.current._highScore2;



        bf.Serialize(file, Game.current);
        file.Close();
        Debug.Log("Saved The File");



    }

    public static void LoadGame()
    {
        if (File.Exists(Application.persistentDataPath + "/SaveGame.dat"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/SaveGame.dat", FileMode.Open);
            Game game = (Game)bf.Deserialize(file);
            file.Close();


            Game.current._highScore1 = game._highScore1;
            Game.current._highScore2 = game._highScore2;
          
            Debug.Log("Loaded The File");
        }
        else
        {
            Debug.Log("File Does Not Exist");
        }
    }
}

Then when ever you want to add an amount to either high score just do this

Game.current._highScore1 += 100; // 100 being what ever score you want to add

And on any of your scripts you want to call

void Awake()
{
SaveLoad.LoadGame();
}

This will load your int variables. And then to save add this to any of your scripts, i usually put it after a score was added…

SaveLoad.SaveGame();

And then when you want this to display the amount pulled from you int _highScore1 you want to have your text field as such…

public Text _highScoreText;

void Start()
{
_highScoreText.text = "" + Game.current._highScore1;

}

Hope this helps!

Just to be fair, there must have been some error with something in your code if it was giving you the wrong values for categories you hadn’t tested.
If the (broken) project is still in tact, and you’re still working on fixing it, you could post it here, as a package - in the thread. :slight_smile:

1 Like