Thought I was doing ok until Text101

Hi! Thanks very much for this course, I am really enjoying it. I thought I was doing ok and understanding everything until this section. But now suddenly I find myself not understanding it. I will go over this section a couple more times but 3 things I can’t seem to understand are:

Earlier in the course, you said each variable has a type, a name, and contains data. But in this section, your variable Text textComponent; doesn’t contain data on the end… Is that because instead of putting it on the end, the place this specific data is stored (story text??) is in Unity so you don’t have to add it on the end of the variable?

Then similarly, earlier you said a string is structured like: string name = “Rick”;

But now is a string and it looks/is structured nothing like that: textComponent.text = (“I am added programmatically!”);

So why is this looking different?

Lastly, as said earlier in course… A string variable means the value will be text… So why is there a text variable too? Is there a difference in use/behaviour?

Sorry to ask so much! I really appreciate any help on these things I am struggling with! Thank you

Hi,

The parentheses are superfluous. Rick added them to emphasise the string expression for beginners. You do not have to use them, and he won’t do that either in the following lectures.

In C#, we have two types: value types and reference types. Value types are all numbers, bool and char.

If you create a new class, it is a reference type. This means that you have to create an object and assign the reference (“link”) to the object to a variable. Text is a class.

Here is an example. In C#, we can create new objects with the new keyword.

void Start() {
    // I assign the reference to the MyClass object that was assigned to mc to a and b
    MyClass mc = new MyClass();
    MyClass a = mc;
    MyClass b = mc;

    // Given there is a property named 'name' inside MyClass
    mc.name = "Object MC";
    a.name = "Object A";
    b.name = "Object B";

    // Now we log the name into our console
    Debug.Log("mc.name: " + mc.name);
    Debug.Log("a.name: " + a.name);
    Debug.Log("b.name: " + b.name);
}

Can you guess the output based on what I said about reference types?

Now the same with a value type:

void Start() {
    int n = 90;
    int a = n;
    int b = n;

    n = 1;
    a = 5;
    b = 27;

    // Now we log the name into our console
    Debug.Log("n: " + n);
    Debug.Log("a: " + a);
    Debug.Log("b: " + b);
}

Can you guess the output based on what I said about value types?

Hi Nina

Thanks for your reply! I understand that Rick didn’t need to add the parenthesis to that string… But even so, the look/structure is still very different to what was taught:

//The structure taught so far which includes the type of variable, its name (in this case "name") and then the data:

string name = "Rick";

//Then the one in question (without the added parenthesis):

textComponent.text = "I am added programatically!";

So is textComponent the name and the .text the type and then the “I am added programatically”; the data? This doesn’t even incude the word string!?

Regarding the rest of your reply, I really appreciate you trying to teach me but I cant figure it out :frowning:

textComponent is the variable of type Text. Or is it of type TextMeshProUGUI? I don’t know. It depends on your code but it does not matter here because I know that both the Text and the TextMeshProUGUI class contain a variable (property) named text.

The type refers to the class. The class contains another variable (property) named text. In C#, you can access public stuff inside a class or an instance of a class via the dot notation. If the class does not contain text, you’d get a compiler error.

Regarding the rest of your reply, I really appreciate you trying to teach me but I cant figure it out

Guess! Replace the ________ with the missing output. If you stay consistent, at least one of the examples will be correct.

// Output for the example with the reference type
mc.name: ________
a.name: ________
b.name: ________

// Output for for the example with the value type
n: ________
a: ________
b: ________

Hi,

A slightly more basic explanation, in case it helps:

  • In programming, the = symbol gives something another value, and what is on both sides of the equals sign have to have the same type.

  • Anything you write in quotations is a string. It doesn’t have to be stored in a variable to be used as a string, it can be used exactly as it is written.

With those things in mind, let’s look at the two lines of code you’re comparing:

string name = "Rick";

This statement is giving a variable called name the value "Rick". In this instance, your code has no idea about the variable name, since it’s never seen it before, so we use the keyword string to tell the code what type it will be.

textComponent.text = "I am added programatically!";

In this case, the code does know about textComponent, and has already been told (in Unity’s own code defining textComponent) that it has a member variable called text which is of type string. Because it already knows this, we don’t have to tell the code what type it will be, so we don’t put string before it.
Then, since we want to give textComponent.text a new value (and it’s a string), we have to put a string on the right hand side of the equals sign. Here, we’re giving it an explicit string value by writing something in quotes - but it’s important to note that we could put anything which is a string in here.

For example, it would be equally okay to write:

string name = "Rick";
textComponent.text = name;

since now you’re applying a string value (name) to a string value (textComponent.text) - and the end result would be that textComponent.text would have the value "Rick".

Does this help?

~ Jonny

1 Like

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).

1 Like

I am getting the gist of it now… Although I am still a bit off understanding everything in your example. I will be back to this thread soon to revisit it once I have learned some more. Thanks again Nina! I really appreciate it :slight_smile:

Ahhh I get it! Thanks so much Jonny! I really appreciate it :slight_smile:

Thanks very much James! Big help :slight_smile:

1 Like

Since this thread already contains multiple explanations: Did any of them help you best? If so, could you please mark it as the solution?


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms