Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Trying to load json files into high score text area

Discussion in 'Scripting' started by justinmed, Apr 3, 2024.

  1. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    79
    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,

    Code (CSharp):
    1. element.points = ScoreNumber.Amount;
    And that is the only error that I received. So, I cannot really test it out until that is resolved.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using UnityEngine;
    5. using TMPro;
    6. using UnityEngine.SocialPlatforms.Impl;
    7.  
    8. public class HighscoreHandler : MonoBehaviour {
    9.     List<HighscoreElement> highscoreList = new List<HighscoreElement> ();
    10.     [SerializeField] int maxCount = 7;
    11.     [SerializeField] string filename;
    12.     public TMP_Text score;
    13.     public TMP_Text PlayerName;
    14.  
    15.     public delegate void OnHighscoreListChanged (List<HighscoreElement> list);
    16.     public static event OnHighscoreListChanged onHighscoreListChanged;
    17.  
    18.     private void Start () {
    19.         LoadHighscores ();
    20.     }
    21.  
    22.     private void LoadHighscores () {
    23.         highscoreList = FileHandler.ReadListFromJSON<HighscoreElement> (filename);
    24.  
    25.         while (highscoreList.Count > maxCount) {
    26.             highscoreList.RemoveAt (maxCount);
    27.         }
    28.  
    29.         if (onHighscoreListChanged != null) {
    30.             onHighscoreListChanged.Invoke (highscoreList);
    31.         }
    32.     }
    33.  
    34.     private void SaveHighscore () {
    35.         FileHandler.SaveToJSON<HighscoreElement> (highscoreList, filename);
    36.     }
    37.  
    38.     public void AddHighscoreIfPossible (HighscoreElement element) {
    39.      
    40.      
    41.      
    42.         string json = File.ReadAllText(Application.dataPath + "/ScoreNumber.json");
    43.         ScoreJSON ScoreNumber = JsonUtility.FromJson<ScoreJSON>(json);
    44.  
    45.         score.text = "" + element.points;
    46.  
    47.         element.points = ScoreNumber.Amount;
    48.      
    49.         json = File.ReadAllText(Application.dataPath + "/Name.json");
    50.         NameJSON name = JsonUtility.FromJson<NameJSON>(json);
    51.         element.playerName = name.name;
    52.  
    53.         PlayerName.text = "" + element.playerName;
    54.  
    55.      
    56.      
    57.      
    58.      
    59.      
    60.         for (int i = 0; i < maxCount; i++) {
    61.             if (i >= highscoreList.Count || element.points > highscoreList[i].points) {
    62.                 // add new high score
    63.                 highscoreList.Insert (i, element);
    64.  
    65.                 while (highscoreList.Count > maxCount) {
    66.                     highscoreList.RemoveAt (maxCount);
    67.                  
    68.                 }
    69.  
    70.                 SaveHighscore ();
    71.  
    72.                 if (onHighscoreListChanged != null) {
    73.                     onHighscoreListChanged.Invoke (highscoreList);
    74.                 }
    75.  
    76.                 break;
    77.              
    78.  
    79.  
    80.             }
    81.         }
    82.     }
    83.  
    84. }
    Code (CSharp):
    1. using System;
    2.  
    3.  
    4. [Serializable]
    5. public class HighscoreElement
    6. {
    7.     public string playerName;
    8.     public int points;
    9.  
    10.     public HighscoreElement(string name, int points)
    11.     {
    12.         playerName = name;
    13.         this.points = points;
    14.     }
    15. }
    16.  
    Next I will post the HighscoreElements.cs just for reference.

    Any help is greatly appreciated.
     
    Last edited: Apr 3, 2024
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,156
    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.
     
    Bunny83 likes this.
  3. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    79
    I agree, I am trying to figure that out. I updated the question but I fixed that part I think with this:

    Code (CSharp):
    1. score.text = "" + element.points;
    2.  
    3.         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.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,156
    Every object in C# has a
    .ToString()
    method that converts the value into its string representation.
     
    Bunny83 likes this.
  5. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    79
    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?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using HS_Scripts;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using TMPro;
    7. using System.IO;
    8. public class HighscoreUI : MonoBehaviour {
    9.     [SerializeField] GameObject panel;
    10.     [SerializeField] GameObject highscoreUIElementPrefab;
    11.     [SerializeField] Transform elementWrapper;
    12.     public TMP_Text score;
    13.     public TMP_Text PlayerName;
    14.  
    15.     List<GameObject> uiElements = new List<GameObject> ();
    16.  
    17.     public void AddScore(HighscoreElement element)
    18.     {
    19.         string json = File.ReadAllText(Application.dataPath + "/ScoreNumber.json");
    20.         ScoreJSON ScoreNumber = JsonUtility.FromJson<ScoreJSON>(json);
    21.  
    22.         score.text = "" + element.points;
    23.  
    24.         score.text = ScoreNumber.Amount;
    25.        
    26.         json = File.ReadAllText(Application.dataPath + "/Name.json");
    27.         NameJSON name = JsonUtility.FromJson<NameJSON>(json);
    28.         element.playerName = name.name;
    29.  
    30.         PlayerName.text = "" + element.playerName;
    31.  
    32.     }
    33.  
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,156
    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.
     
    Bunny83 likes this.