Unable to assign value to Text
I'm trying to add a value to a "Text" type variable to later use it with UI Text component.
Here is my code:
#pragma strict
import UnityEngine.UI;
var countText : Text;
function Start() {
countText.text = "content";
}
NullReferenceException: Object reference not set to an instance of an object (on line 8). What does this mean and how can I fix it?
@skrail countText = null (or that countText does not contain a variable "text") is what it means. countText doesn't reference to any object because you never assign a prefab or instantiate a new GameObject/Text.
I'm not sure what you're really trying to do. If you already have a text-component you wnat to change you should drag it to the variable-slot in the Editor.
If you just want to save text, why not use a string?
And if you want to create the text-component dynamically you should still just save the "content" in a string and then set the value after creating the text-component. You can create a new component using AddComponent().
Your answer
Follow this Question
Related Questions
uGUI Text draw calls 1 Answer
null reference exception with text 0 Answers
Show a text in the position of a GameObject 0 Answers