I had the score displaying properly in my game but decided the code was kind of all over the place. So I deleted it all and started making a ScoreManager script but now I’m having errors.
Here is the script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public Text scoreText;
public float score = 0f;
// Use this for initialization
void Awake ()
{
scoreText = GetComponent<Text> () as Text;
}
// Update is called once per frame
void Update ()
{
scoreText.text = score.ToString();
}
}
The code in the Update is throwing a Object reference not set to an instance of an object error.
Here is the inspector for the empty object in the hierarchy that has the ScoreManager script attached to it:
I’ve assigned my scoreText variable here by plugging in the UI Text element that is also in my hierarchy as that is how I got the code to display before.
What am I missing?