I see your confusion, you see String name = "Rick"
and think, oh so String is a variable holding words.
Then you see textComponent.text = "I am added programmatically"
and think, wait…but that does not have the String variable, instead it is .text?
Let’s break this down. Forwarning, I am going to go a bit deeper into how Unity does things, and I am going to start a little higher up in the architecture. I think this will help to understandt the whole .text
thing a bit better. So lets start with…Components?
In Unity, a COMPONENT is a Class, well, it’s technically an INSTANCE of a class. Instance: meaning…well, fancy for “it’s a copy”, the original is called the base class and we never actually touch it or modify it. Unity further defines components as “the functional part of a gameobject”…like that is DEEP.
A component, like any class, contains properties
and methods
within it. An easy example is, When you move a gameobject along the x axis in the editor, you are modifying the Transform
component, specifically the transform.position.x
. You can see the changes in the inspector. In fact, all those fields you can type in and change, those are all properties
of the Transform class. So .position
is a property of transform.
Now, if you are a little confused about what a property
actually is: for all intents and purpose, its a variable, that is the easiest way to think of it. Technically it is a variable that has a getter and a setter(to allow access from the outside), but…just think property = variable for now.
Now, when you want to use or mess with one of those components within a script (instead of just using the inspector) you have to reference to it first (tell the code where it is and give it a name). The most basic way is with this line of code:
someName = GetComponent<Type>()
; Type is the component type (i.e Transform, Text)
There are other several other ways to do this as well, depending on the situation, but this is the most common to reference a component attached to the same gameobject that the script is attached to.
Using that line of code above, a simple way to get an attached Text
component could be:
someName = GetComponent<Text>()
In our case “someName” is actually “textComponent”.
textComponent
is just what Rick named the Text Component reference that was attached to the gameobject. It could have been named blueDinosour and it would not have mattered. But in our case, we have a reference to the attached Text Component called, simply enough, textComponent because keep it simple right.
Other common Components are Transform, MeshRenderer, Rigidbody.
Now for the second part. The Text component has it’s own properties. Just like the Transform component has the .position
property, the Text Component has, you guessed it, a .text
property! And just like doing something like transform.position.x = 5
would set the position of the gameobject to 5 on the x axis, textComponent.text = "I am added programmatically"
sets the .text property, of textComponent to I am added programmatically.
But what IS .text
? Like, Really?
Well. Remember when I said components are instances of a Class? Well, the base class (the original, the first one,the one all other Text components are copied from) defined that .text
IS of Type string. If fact, if you highlight the code in most editors (not sure about Mono develop), it will actually tell you that it is, in fact, of Type string. All that is just predefined deeper within unity, within the base class. So in the simplest form, .text is a Variable, of Type String. So we now have a variable of type String with data in it!
But wait! Variables also need names! What is it’s NAME! Hmm, well that is a bit more convoluted, but for our purpose, it’s simply named textComponent.text. Why? Well, because we did not name it ourselves and so all we can do is tell the program where to find it (in memory). The compiler sees textComponent.text and knows where to go.
Now if you DID want to name it? Well, you can and you can’t. You could take the data inside and put it in a String like this: nameMe = textComponent.text.
And with that, we sorta came full circle as we have a variable with all the criteria:
Has a name: nameMe
A Type: String, as this was derived from the fact .text was of Type string in the base class.
Data: “I am added programmatically”
Now, a caveat: you can’t just change this new variable and change the .text component, because you have to actually apply any changes back to the .text property. But, you can now freely modify nameMe:
nameMe = "A whole new sentance."
And then when ready put those words to the screen by putting them back into textComponent.text:
textComponent.text = nameMe
This is a bit long winded, and I tried to stay within Unity for my examples instead of just general C#. Hope this did not confuse you more.
For more information, check out Unity’s doc’s on the Text component
In that link you will see all the properties associated with the Text Component, including .text. Click any of them to see code on how they are used.
And some useless knowledge for the day about string:
a String is reference type. It is actually an immutable sequence of characters, and you can’t change it!
myString = “Replace the old words with this new words” does not in fact change anything. In reality it creates a NEW object in memory, makes myString reference this new object, fills it with the new characters, and then makes the old object eligible to be deleted (during garbage collection).