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.