Hi all,
I am a little baffled by this one, mind you I am baffled by a lot of things these days, so one more thing won’t hurt I guess ![]()
I have added the UnityEngine.UI namespace via a “using” directive then I added a private Text class member, I tried to make it public and drag and drop the Text component to it but wasn’t able to drop it, it wouldn’t take it, the mouse icon remained “forbidden” ![]()
I then associated the private member called “_text” to the component in the “Start” function like this:
_text = GetComponentInChildren ();
and proceeded to set its text like this:
_text.text = _scoreValue.ToString ();
I have another 2 functions built by myself which can make changes to the text value of the _text member, unfortunately I cannot use _text.text = some_string;, I keep getting a NullReferenceException error: Object reference not set to an instance of an object. The only way I can change the text in these functions is to do this:
GetComponentInChildren().text = some_string;
I do not understand why I have to do this everytime and why my _text member can’t keep a reference to the text component, it’s driving me nuts.
I also don’t understand why when I make the field public I cannot drop it into the gameobject’s inspector window.
The Text component is inside a Canvas component which itself is inside the game object associated to the C# script, hence the GetComponentInChildren function used and not the GetComponent function.
Regards
Crouz