easy UI text question text doesnt update
hi im trying to update a text from a value from another script but nothing gets updated heres what my script is doing
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class coinTextUpdate : MonoBehaviour {
Text coinText;
public int coinsCollected;
coincollision coinScript;
// Use this for initialization
void Awake () {
}
// Update is called once per frame
void Update () {
//coinText = coinText.GetComponent<Text>();
coinScript = gameObject.GetComponent<coincollision>();
coinsCollected = coinScript.coinCollected;
coinText.text = "Score: " + coinScript.coinCollected.ToString();
}
}
the script is attached to the text object attached to a canvas and the error i get is
NullReferenceException: Object reference not set to an instance of an object
coinTextUpdate.Update () (at Assets/scripts/coinTextUpdate.cs:16)
and im sure someone will say something like that error is telling you exactly what you need to know! but i just cannot see it any help please?
Answer by corn · Jan 17, 2016 at 07:13 PM
Alright, so let's see what's wrong here. As you know, the faulty line is :
coinText = coinText.GetComponent<Text>();
coinText is already a Text, so there's no point in trying to get its Text component. I guess what you meant to do was to get a Text attached to the same GameObject your script is :
coinText = GetComponent<Text>();
What happens here is that when the script hits line 16, coinText has not been initialized, it is null. So you cannot call GetComponent on coinText, hence the NullRefException.
So you just have to remove coinText.
and your script will work. However, using GetComponent in Update is a bad habit, so this script should be improved a bit. Put all your GetComponent calls in Awake : you only need to get references to your other Components once, not every frame.
void Awake ()
{
// Always get your references in Awake or Start, not Update
coinText = GetComponent<Text>();
coinScript = GetComponent<coincollision>();
}
void Update ()
{
// It's good practice to check if GetComponent found what you were looking for
if (coinText != null && coinScript != null)
{
coinsCollected = coinScript.coinCollected;
coinText.text = "Score: " + coinScript.coinCollected.ToString();
}
}
Another way of finding these components would be to Serialize them and set them in the Inspector via drag and drop.
public class coinTextUpdate : MonoBehaviour
{
[SerializeField]
Text coinText;
[SerializeField]
coincollision coinScript;
...
}
Then you will be able to see a Text field and a coincollision field in the Inspector. Simply drag and drop the GameObjects to which the Text and coincollision are attached (in this case, the same GameObject this script is attached to), and you're good to go, and you can even remove the GetComponent calls in Awake since you won't need them anymore.
this is a beautiful answer i'm new to Unity but obviously i know finding an object is an intensive method and i also know that update is called every frame but didnt really put the two together and think how damaging it is so thank you very much ill also be taking your advice and serializing all the things! thanks again
Your answer
Follow this Question
Related Questions
UI Text rendering in front and behind planes 0 Answers
UI TEXT not showing up in game mode 1 Answer
GameObject destroyed when changing scene 1 Answer
Object reference not set to instance of an object? 1 Answer
null reference exception with text 0 Answers