[Solved] Not really understanding text.text and dragging it into text

Maybe this is explained later in the course but id really like to know what im doing…
is text a class in the “using UnityEngine.UI;” namespace? and i create an instance of that class: text.text ??
how come a new option suddenly apears in the unity GUI? and what actually happens when i drag the text element into it?

Im not near my gamedev pc (which has unity installed), but usually appears a new GUI into the inspector when you initialize something as public, if that was the case, then you are telling the engine that this variable can be accessed by elements outside the script, including through the inspector (that’s why popped up a new GUI in the inspector).

There is a class named Text inside the UnityEngine.UI library, you are accessing it as lowercase “text” because you initialized a variable named text as a Text type of variable when you written public Text text.

Now the engine knows that the variable text is a Text type, but it still don’t have any value addressed to it, and since you want to make it change what is inside the Text component inside the GameObject you need to initialize it as public (as you did) and add the path to the component inside the inspector, or you could address a value to it through the script with this line:

text = GetComponent<Text>();

This says that the variable text that you just created is the component Text inside this script’s GameObject.

Now the engine knows that the text variable is the Text component inside the GameObject, but there are plenty of different methods inside the Text class,Text.text is just the path to the words written inside the text component (there are other methods besides Text.text, such as Text.font and etc.), this is why you use text.text (the text method inside the text variable of type Text that you created and linked to your GameObject’s text component).

It’s a little bit crazy :vulcan: but I hope that you get it now, let me know if I didn’t make myself understandable enough, English isn’t my first language.

3 Likes

Thanks so much, you should be a teacher! :slight_smile:

Thank you for the compliment Luckie :grinning:

I’m far from being a teacher, still have plenty to learn, I didn’t even finished the unity course yet :sweat_smile:

Privacy & Terms