- Home /
My void Start won't work
Hello everyone. I'm basically a noob on unity, so i need your help on this one. I'm modifying a code i scrape from a codemonkey tutorial. They idea is to save a value at the end of each game, and compare this value to the previous in order for the player to understand is improvement compared to his previous play. My problem is that my second void sart (in the Highscores Private class) won't start when i launch the game. If everyone have an explanation i will gladly hear it. Thanks a lot, the code is below. using System.Collections; using System.Collections.Generic; using UnityEngine; using CodeMonkey; using CodeMonkey.Utils; using System.IO;
[System.Serializable] public class GameHandler : MonoBehaviour {
private Transform entryContainer;
private Transform entryTemplate;
private int MaxObjCount = 2;
private List<Transform> highscoreEntryTransformList;
private void Awake()
{
string jsonString = PlayerPrefs.GetString("highscoreTable");
Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString);
if (highscores == null)
{
// There's no stored table, initialize
Debug.Log("Initializing table with default values...");
AddHighscoreEntry(0);
AddHighscoreEntry(0);
// Reload
jsonString = PlayerPrefs.GetString("highscoreTable");
highscores = JsonUtility.FromJson<Highscores>(jsonString);
}
highscoreEntryTransformList = new List<Transform>();
}
public static void AddHighscoreEntry(float score)
{
// Create HighscoreEntry
// PlayerPrefs.DeleteAll();
HighscoreEntry highscoreEntry = new HighscoreEntry { score = score};
// Load saved Highscores
string jsonString = PlayerPrefs.GetString("highscoreTable");
Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString);
if (highscores == null)
{
// There's no stored table, initialize
highscores = new Highscores()
{
highscoreEntryList = new List<HighscoreEntry>()
};
}
// Add new entry to Highscores
highscores.highscoreEntryList.Add(highscoreEntry);
/* while (highscoreEntryTransformList.Count >MaxObjCount)
{
if (highscoreEntryTransformList[0] != null)
Destroy(highscoreEntryTransformList[0].highscores);
highscoreEntryTransformList.RemoveAt(0);
}*/
// Remove Old entry to Highscores
// Save updated Highscores
string json = JsonUtility.ToJson(highscores);
PlayerPrefs.SetString("highscoreTable", json);
PlayerPrefs.Save();
// Debug.Log(PlayerPrefs.GetString("highscoreTable"));
}
private class Highscores : MonoBehaviour
{
public List<HighscoreEntry> highscoreEntryList;
public void Start()
{
Debug.Log(highscoreEntryList.Count);
DeletePreviousScore();
}
private void DeletePreviousScore()
{
if (highscoreEntryList.Count > 2)
{
highscoreEntryList.RemoveAt(0);
}
}
}
/*
* Represents a single High score entry
* */
[System.Serializable]
private class HighscoreEntry
{
public float score;
}
}
Answer by ShadyProductions · Jun 24, 2020 at 04:13 PM
You have your Highscores monobehaviour class inside another monobehaviour class, how do you expect it to work?
First of all, thank you for your reply.The thing is that if i don't use monobehaviour with this class, unity won't recognize the Start void, that's why i make this mistake. Any idea on how to solve this? I have first try to move the Highscores class in a different script. But when i do that, the script won't recognize the HighscoreEntry term in the public List highscoreEntryList; That's why i try to make it all work on the sazme script. Again thank you for your time
You need to make the class public and make sure you import the namespace it is part off.