I’m working on the data persistence tutorials in Unity Learn. I’m trying to get data from this script in the menu scene but it’s not working and I’m receiving no errors.
public class HiScores : MonoBehaviour
{
public static HiScores Instance;
public float hiScore;
public string playerName;
private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
}
and send some data to this part of a script in main scene
public class MainManager : MonoBehaviour
{
else if (m_GameOver)
{
HighScoreText.text = "High Score " + HiScores.Instance.playerName + HighScore;
if (Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
and the data in the first script comes from this script
public class MenuUIHandler : MonoBehaviour
{
public string player;
public void NewPlayerName(string player)
{
HiScores.Instance.playerName = player;
}
public void StartNew()
{
SceneManager.LoadScene(1);
}
public void ReadStringInput(string name)
{
player = name;
Debug.Log(name);
}
}
in the MainManager script, I tried using HiScores.Instance.playerName to get the data from the input field to show in the high score text but it doesn’t work. I’m stumped on this. After this, I still have to solve the problem of saving that data so that it remains when the game is closed and opened again.
I’ve also tried referencing the script in a serialized field, setting it in the inspector and then attempted to reference the playerName string but received an error… something about it being a string but I don’t remember