FlashX
September 27, 2015, 12:16am
1
Hi all,
Got a stupid question…
How do i talk to a UI object? Ive got the below code but I cant see what I’m doing wrong… Any ideas?
using UnityEngine.UI;
void Start() {
theScore = GameObject.Find("Canvas/score");
GameObject theScore = GetComponent<UnityEngine.UI.Text>();
theScore.text = "hello";
}
Well, you’re setting
theScore = GameObject.Find("Canvas/score");
However, immediately after that you redefine “theScore” variable as a new GameObject and are setting it to the text component of the current GameObject via this line:
GameObject theScore = GetComponent<UnityEngine.UI.Text>();
I assume what you want to do is something like this:
Text theScore = GameObject.Find("Canvas/score").GetComponent<Text>();
theScore.text = "hello";