I am having a weird problem, and this is alot of code to go through but it really all has something to do with the ScoreBoardEntryData, ScoreBoardUI; and the AddScore void in the Scoreboard.cs script. But, as soon as I unpack the prefab that it is in, it loses its ability to number going down as a high score table should. I got this code from a tutorial and can get it to work perfectly fine so long as I do not need to replicate it, but I need multiple high score boards and it is so locked in. I cannot figure out why it loses the ability to count when the prefab is unpacked. It is so buggy. If I undo the unpacks, it still cannot count. And I cannot even reimport it back in functional. It loses ability to count freshly imported after that as well.
Scoreboard.cs:
using System.IO;
using UnityEngine;
namespace DapperDino.Scoreboards
{
public class Scoreboard : MonoBehaviour
{
[SerializeField] private int maxScoreboardEntries = 5;
[SerializeField] private Transform highscoresHolderTransform = null;
[SerializeField] private GameObject scoreboardEntryObject = null;
[Header("Test")]
[SerializeField] private string testEntryName = "New Name";
[SerializeField] private int testEntryScore = 0;
private string SavePath => $"{Application.persistentDataPath}/highscores.json";
private void Start()
{
ScoreboardSaveData savedScores = GetSavedScores();
UpdateUI(savedScores);
SaveScores(savedScores);
}
[ContextMenu("Add Test Entry")]
public void AddTestEntry()
{
string json = File.ReadAllText(Application.dataPath + "/NewName.json");
NameJSON Name = JsonUtility.FromJson<NameJSON>(json);
string testEntryName = Name.name;
json = File.ReadAllText(Application.dataPath + "/ScoreNumber.json");
ScoreJSON ScoreNumber = JsonUtility.FromJson<ScoreJSON>(json);
int testEntryScore = ScoreNumber.Amount;
AddEntry(new ScoreboardEntryData()
{
entryName = testEntryName,
entryScore = testEntryScore
});
}
public void AddEntry(ScoreboardEntryData scoreboardEntryData)
{
ScoreboardSaveData savedScores = GetSavedScores();
bool scoreAdded = false;
//Check if the score is high enough to be added.
for (int i = 0; i < savedScores.highscores.Count; i++)
{
if (testEntryScore > savedScores.highscores[i].entryScore)
{
savedScores.highscores.Insert(i, scoreboardEntryData);
scoreAdded = true;
break;
}
}
//Check if the score can be added to the end of the list.
if (!scoreAdded && savedScores.highscores.Count < maxScoreboardEntries)
{
savedScores.highscores.Add(scoreboardEntryData);
}
//Remove any scores past the limit.
if (savedScores.highscores.Count > maxScoreboardEntries)
{
savedScores.highscores.RemoveRange(maxScoreboardEntries, savedScores.highscores.Count - maxScoreboardEntries);
}
UpdateUI(savedScores);
SaveScores(savedScores);
}
private void UpdateUI(ScoreboardSaveData savedScores)
{
foreach (Transform child in highscoresHolderTransform)
{
Destroy(child.gameObject);
}
foreach (ScoreboardEntryData highscore in savedScores.highscores)
{
Instantiate(scoreboardEntryObject, highscoresHolderTransform).GetComponent<ScoreboardEntryUI>().Initialise(highscore);
}
}
private ScoreboardSaveData GetSavedScores()
{
if (!File.Exists(SavePath))
{
File.Create(SavePath).Dispose();
return new ScoreboardSaveData();
}
using (StreamReader stream = new StreamReader(SavePath))
{
string json = stream.ReadToEnd();
return JsonUtility.FromJson<ScoreboardSaveData>(json);
}
}
private void SaveScores(ScoreboardSaveData scoreboardSaveData)
{
using (StreamWriter stream = new StreamWriter(SavePath))
{
string json = JsonUtility.ToJson(scoreboardSaveData, true);
stream.Write(json);
}
}
}
}
ScoreboardEntryData.cs:
using System;
namespace DapperDino.Scoreboards
{
[Serializable]
public struct ScoreboardEntryData
{
public string entryName;
public int entryScore;
}
}
ScoreboardEntryUI.cs:
using TMPro;
using UnityEngine;
using System.IO;
namespace DapperDino.Scoreboards
{
public class ScoreboardEntryUI : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI entryNameText = null;
[SerializeField] private TextMeshProUGUI entryScoreText = null;
public void Initialise(ScoreboardEntryData scoreboardEntryData)
{
entryNameText.text = scoreboardEntryData.entryName;
entryScoreText.text = scoreboardEntryData.entryScore.ToString();
}
}
}
ScoreboardSaveData.cs:
using System;
using System.Collections.Generic;
namespace DapperDino.Scoreboards
{
[Serializable]
public class ScoreboardSaveData
{
public List<ScoreboardEntryData> highscores = new List<ScoreboardEntryData>();
}
}