Alright so here is my code and I am getting errors. I need help guys. Below is my code.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static int score = 0;
public Text scoreText;
public Text bestText;
void Start ()
{
DontDestroyOnLoad (gameObject);
DontDestroyOnLoad (scoreText);
DontDestroyOnLoad (bestText);
DontDestroyOnLoad (score);
if (score > PlayerPrefs.GetInt (Application.loadedLevel + "Best"))
{
PlayerPrefs.SetInt (Application.loadedLevel + "Best", score);
//Play new high score animation
scoreText.text = "" + score;
bestText.text = "" + PlayerPrefs.GetInt (Application.loadedLevel + "Best");
}
}
void Update ()
{
scoreText.text = "" + score;
bestText.text = "" + PlayerPrefs.GetInt (Application.loadedLevel + "Best");
}
}
Here are the errors:
MissingReferenceException: The object of type ‘Text’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.EventSystems.UIBehaviour.IsActive () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/UIBehaviour.cs:22)
UnityEngine.UI.Graphic.SetVerticesDirty () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Graphic.cs:93)
UnityEngine.UI.Text.set_text (System.String value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Text.cs:140)
ScoreManager.Update () (at Assets/Scripts/ScoreManager.cs:29)