Suggestion relating to Text object

Hi I have recently enrolled into this course to start my journey into Game Development and I just wanted to suggest to make apparent/obvious a small detail that I didn’t clearly absorb in the lecture of this topic.

What is it
This is the part where it wasn’t clearly explained as to why a particular exposure of a unity object to the C# script, particularly the TEXT has to be named “text” i.e. public Text text; In my initial thought, the name should have been what the object’s name in unity was or at least instantiate the variable with a function call to linking an object but I was wrong.

Below is an excerpt from my build in unity (hopefully the upload function from copy and paste works)
image

If you look at the image, my actual Unity object is named Guess which has a type TEXT and I linked it to the recently created empty UI to the number wizard script. I have encircled the word Text in the Inspector. In my mind, the statement public Text text; actually refers to the Text after the public and, correct me if I’m wrong, the “text” name can be any name after that declaration.

Suggestion
I just feel, there should be a little bit more discussion about this as I believe that this fundamental knowledge will go a long way in understanding codes.

Thank you :wink:

Yes you are correct. “Text” with a capital T is the class name / variable type whereas “text” with a lowercase t is a variable name. You could call that whatever you feel like. The Text keyword on the inspector is just to tell you what class it is expecting to be passed. So text in Text text. will refer to the Text Guess that you’ve attached to it.

Note the variable name can’t be anything but it can be most things. You can’t have a variable name that is a class name or another element of existing code. So you couldn’t have your variable name as “Text” as this is the class, similarly it couldn’t be “int” or “this” as these refer to specific things but for the most part you can use most words you can think of.

And for what it’s worth I agree it’s a bit confusing, I really don’t like that we start of by using variable names that match the class names as the subtlety of it can easily be lost. I had a bit of trouble with GameObject, which is the general class and gameObject which is the specific game object you are already talking about.

Privacy & Terms