Print () vs. text.text understanding

I would like to know the difference between using the Print () and text.text in this lesson.

Why can we not use print () over text.text in lesson #30?

When would we use print () over text.text and vis versa?

1 Like

Hello @Mike_S,

Print() is a method within the Unity framework, it’s just a shortcut for the Debug.Log() method and is used for writing to the console.

text.text needs a little bit of a break down to explain.

In your code you will be familiar with seeing this;

public Text text;

There are three parts here, lets run through each;

  • public this is the access modifier, this determines the scope of the variable/field. In this case, you are setting your variable to public so that you can interact with it within the Unity editor.

  • Text this is the type, this will match the UI Text game object that you placed in your scene. Other types include int, string, float.

  • text this is the variable of type Text, this is the name which will be used within your code to reference it.

Now, this is where things get a little messy…

The type Text has a set of variables/fields/properties/methods. One of those is actually called “text”, it’s the property which allows you to both get and set the text which will actually be displayed.

So when you write text.text = "Mike"; you are actually say, “Set the text property on my text variable, which is of type Text, to Mike”.

Banging your head against the wall yet? :slight_smile:

One thing which may make this a little simpler to read/understand is perhaps renaming the UI Text game object to “Story”, and then in your code, change this;

public Text text;


`public Text story;"

Then later, when you use it, you can write story.text = "Hello Mike"; for example.

I hope this helps. :slight_smile:

See also;

1 Like

Hey Rob I appreciate your detailed explanation on this as it definitely clears things up for me. :slight_smile:

1 Like

You’re more than welcome Mike, the links are just for some further reading if you should so wish. Anything else, just post :slight_smile:

Thank you so much for explaining this in such detail! I like to fully understand each line of code and this just was 't explained in the video. I think the instructor using text as the varible name made it more confusing. So, thanks a Million! :grin:

1 Like

You’re very welcome @BassMonkey :slight_smile:

Privacy & Terms