Trying to load json files into high score text area

This is kind of a big question to go through, but I thought it could not hurt to ask, any help is appreciated. I am new to coding and I am trying to work with a high score system that I got from a tutorial.

This tutorial series in fact. But, I am trying to gear everything into my own project and am trying to configure the string playerName and int points into text boxes that load up after an enter name pop up is satisfied. So when you click enter, the name saves to a json file, and takes the name json file and plugs it into the name slot and the score json file is plugged into the score slot. The coding that I am trying to implement this in is in the HighscoreHandler.cs. The string and int for the values of the entries are designated in the HighscoreElements.cs.

I will first show the HighscoreHandler.cs script, I tried putting in the code to load the json files through the enter button into the top of the public void AddHighscoreIfPossible() section. You will see that I had an issue trying to tell the int of points to = the value of the json script.

which is this line,

element.points = ScoreNumber.Amount;

And that is the only error that I received. So, I cannot really test it out until that is resolved.

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using TMPro;
using UnityEngine.SocialPlatforms.Impl;

public class HighscoreHandler : MonoBehaviour {
    List<HighscoreElement> highscoreList = new List<HighscoreElement> ();
    [SerializeField] int maxCount = 7;
    [SerializeField] string filename;
    public TMP_Text score;
    public TMP_Text PlayerName;

    public delegate void OnHighscoreListChanged (List<HighscoreElement> list);
    public static event OnHighscoreListChanged onHighscoreListChanged;

    private void Start () {
        LoadHighscores ();
    }

    private void LoadHighscores () {
        highscoreList = FileHandler.ReadListFromJSON<HighscoreElement> (filename);

        while (highscoreList.Count > maxCount) {
            highscoreList.RemoveAt (maxCount);
        }

        if (onHighscoreListChanged != null) {
            onHighscoreListChanged.Invoke (highscoreList);
        }
    }

    private void SaveHighscore () {
        FileHandler.SaveToJSON<HighscoreElement> (highscoreList, filename);
    }

    public void AddHighscoreIfPossible (HighscoreElement element) {
      
      
      
        string json = File.ReadAllText(Application.dataPath + "/ScoreNumber.json");
        ScoreJSON ScoreNumber = JsonUtility.FromJson<ScoreJSON>(json);

        score.text = "" + element.points;

        element.points = ScoreNumber.Amount;
      
        json = File.ReadAllText(Application.dataPath + "/Name.json");
        NameJSON name = JsonUtility.FromJson<NameJSON>(json);
        element.playerName = name.name;

        PlayerName.text = "" + element.playerName;

      
      
      
      
      
        for (int i = 0; i < maxCount; i++) {
            if (i >= highscoreList.Count || element.points > highscoreList[i].points) {
                // add new high score
                highscoreList.Insert (i, element);

                while (highscoreList.Count > maxCount) {
                    highscoreList.RemoveAt (maxCount);
                  
                }

                SaveHighscore ();

                if (onHighscoreListChanged != null) {
                    onHighscoreListChanged.Invoke (highscoreList);
                }

                break;
              


            }
        }
    }

}
using System;


[Serializable]
public class HighscoreElement
{
    public string playerName;
    public int points;

    public HighscoreElement(string name, int points)
    {
        playerName = name;
        this.points = points;
    }
}

Next I will post the HighscoreElements.cs just for reference.

Any help is greatly appreciated.

When pointing out syntax errors, tell us the exact line of code its happening on because I have no idea where in your code you’re referring to here.

Also JsonUtility can’t serialize/deserialised naked collections. If you want to read/write a collection of high scores, you need to wrap those in another class.

In principle it should just be as simple as:

  • Read data from disk
  • Generate UI elements for each high score

Doesn’t need to be any more complicated than that.

1 Like

I agree, I am trying to figure that out. I updated the question but I fixed that part I think with this:

score.text = "" + element.points;

        score.text = ScoreNumber.Amount;

However the public void is greyed out and not showing up in the OnButtonClick function in the UI. I think the code makes sense in that it does what you say a simple high score system should be, but I need to figure out how to get it to show up in the OnButtonClick function to try it out first.

Every object in C# has a .ToString() method that converts the value into its string representation.

1 Like

For some reason I cannot get the command to show up in the function section of on click, I tried changing the public void over to the HighscoreUI script and having it by itself, but it still did not show up. Can I not have an element in a public void if I want it to show up?

using System.Collections;
using System.Collections.Generic;
using HS_Scripts;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.IO;
public class HighscoreUI : MonoBehaviour {
    [SerializeField] GameObject panel;
    [SerializeField] GameObject highscoreUIElementPrefab;
    [SerializeField] Transform elementWrapper;
    public TMP_Text score;
    public TMP_Text PlayerName;

    List<GameObject> uiElements = new List<GameObject> ();

    public void AddScore(HighscoreElement element)
    {
        string json = File.ReadAllText(Application.dataPath + "/ScoreNumber.json");
        ScoreJSON ScoreNumber = JsonUtility.FromJson<ScoreJSON>(json);

        score.text = "" + element.points;

        score.text = ScoreNumber.Amount;
       
        json = File.ReadAllText(Application.dataPath + "/Name.json");
        NameJSON name = JsonUtility.FromJson<NameJSON>(json);
        element.playerName = name.name;

        PlayerName.text = "" + element.playerName;

    }

Because it has a parameter that the OnClick unity event doesn’t support. It only supports basic data types like int, bool etc, and Unity objects.

You would have to hook these up via code then.

Or just have a parameterless method that does the work.

1 Like