Hello, all I want to do is calling the AddHighscoreEntry method, but as soon as I call, the error appears.
I already searched the internet for solutions.
What I can say by now:
-Theres no empty field in the inspector that has to be filled
-Theres no other Reference to that script in scene
I have no more ideas left.
[code]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HighscoreSystem : MonoBehaviour
{
Transform rankingTemplate;
Transform entryContainer;
List highscoreEntryTransformList = new List();
private void Awake() {
entryContainer = transform.Find("HighscoreContainer");
rankingTemplate = entryContainer.Find("RankingTemplate");
rankingTemplate.gameObject.SetActive(false);
string jsonString = PlayerPrefs.GetString("highscoreTable");
Highscores highscores = JsonUtility.FromJson(jsonString);
//sorting algorithm
for (int i = 0; i < highscores.highscoreEntryList.Count; i++){
for (int j = i+1; j < highscores.highscoreEntryList.Count; j++){
if(highscores.highscoreEntryList[j].score < highscores.highscoreEntryList[i].score){
//swap
HighscoreEntry temp = highscores.highscoreEntryList[i];
highscores.highscoreEntryList[i] = highscores.highscoreEntryList[j];
highscores.highscoreEntryList[j] = temp;
}
}
}
//delete above 10 entrys
if (highscores.highscoreEntryList.Count > 10){
for (int h = highscores.highscoreEntryList.Count; h>10; h--){
highscores.highscoreEntryList.RemoveAt(10);
}
}
newInstantiation();
}
public void newInstantiation(){
string jsonString = PlayerPrefs.GetString("highscoreTable");
Highscores highscores = JsonUtility.FromJson(jsonString);
foreach (HighscoreEntry highscoreEntry in highscores.highscoreEntryList){
createHighscoreEntryTransform(highscoreEntry, entryContainer, highscoreEntryTransformList);
}
}
//entryInstantiation
void createHighscoreEntryTransform(HighscoreEntry highscoreEntry, Transform container, List transformList){
Transform instantiatedRankingTemplate = Instantiate(rankingTemplate, container);
RectTransform entryRankingTemplate = instantiatedRankingTemplate.GetComponent();
RectTransform rectOfRankingTemplate = rankingTemplate.GetComponent();
float yAbstand = 22;
entryRankingTemplate.anchoredPosition = new Vector2(0, rectOfRankingTemplate.anchoredPosition.y - yAbstand * transformList.Count);
instantiatedRankingTemplate.gameObject.SetActive(true);
int rank = transformList.Count + 1;
string rankString;
switch(rank){
case 1: rankString = "1st"; break;
case 2: rankString = "2nd"; break;
case 3: rankString = "3rd"; break;
default: rankString = rank + "th"; break;
}
float score = highscoreEntry.score;
string name = highscoreEntry.name;
instantiatedRankingTemplate.Find("Rank").GetComponent().text = rankString;
instantiatedRankingTemplate.Find("Score").GetComponent().text = score.ToString("F2");
instantiatedRankingTemplate.Find("Name").GetComponent().text = name;
instantiatedRankingTemplate.Find("Background").gameObject.SetActive(rank % 2 == 1);
transformList.Add(instantiatedRankingTemplate);
}
public void AddHighscoreEntry(int score, string name){
//create Highscore Entry
HighscoreEntry highscoreEntry = new HighscoreEntry { score = score, name = name};
//load highscore list
string jsonString = PlayerPrefs.GetString("highscoreTable");
Highscores highscores = JsonUtility.FromJson(jsonString);
//add new entry
highscores.highscoreEntryList.Add(highscoreEntry);
//sorting algorithm
for (int i = 0; i < highscores.highscoreEntryList.Count; i++){
for (int j = i+1; j < highscores.highscoreEntryList.Count; j++){
if(highscores.highscoreEntryList[j].score < highscores.highscoreEntryList[i].score){
//swap
HighscoreEntry temp = highscores.highscoreEntryList[i];
highscores.highscoreEntryList[i] = highscores.highscoreEntryList[j];
highscores.highscoreEntryList[j] = temp;
}
}
}
//delete above 10 entrys
if (highscores.highscoreEntryList.Count > 10){
for (int h = highscores.highscoreEntryList.Count; h>10; h--){
highscores.highscoreEntryList.RemoveAt(10);
}
}
//safe new highscore list
string json = JsonUtility.ToJson(highscores);
PlayerPrefs.SetString("highscoreTable", json);
PlayerPrefs.Save();
//delete old scoreboard text
Object[] allObjects = FindObjectsOfType(typeof(GameObject));
foreach(GameObject obj in allObjects) {
if(obj.transform.name == "RankingTemplate(Clone)"){
Destroy(obj);
}
}
//new scoreboard text
highscoreEntryTransformList.Clear();
newInstantiation();
}
[System.Serializable] public class Highscores
{
public List highscoreEntryList;
}
[System.Serializable] public class HighscoreEntry{
public int score;
public string name;
}
}
[/code]
↧