Public Text text declaration?

Hello.

Sorry if this is the wrong place to post a question like this, but there is a line of code that we write during one of the lectures while making the prison game that I do not full understand. It works just fine, but it drives me mad when I have written something that I don’t fully appreciate!

Anyway the line is:

public Text text;

It’s the first line, but I’m a little confused as to how it works. We are declaring Text as public, I suppose. The problem is, we have an object called ‘Text’ (our text box), but Text also contains a field called ‘Text’. So I’m confused which we are pointing to in this case. As to what the lowercase ‘text’ is for I have no idea.

Anyway, I’m aware this probably isn’t very clear, but if you’ve done this section then I’m sure you know what I mean! Thanks!

Thom

2 Likes

Hi @MooseLucky,

public Text text

public is the access modifier, by stating it’s public it will be available to objects outside of itself
Text is a Type
text is your variable
text.text is the text property of your variable, of type Text

So, what you have is, a variable called text of type Text which has been set to public.

You have Text as the Type because you will be passing in a Text object into it, via the Inspector. If this wasn’t given the Public access modifier the Inspector wouldn’t be able to see/access it.

This question comes up fairly often and in part I think is due to the naming of objects. For example, if you named your variable story you would then pass your Text UI object into into, and within the code refer to it via story.text which may make a little more sense reading it.

Hope this helps.

2 Likes

Thanks @Rob,

This was really helpful!

So following your explanation, I had a little play around with the code. And this was my understanding (please let me know if it’s correct!).

public, as you’ve said means that unity can see the variable.
Text, as the type is preexisting.
text, again as you’ve said is the variable.

So we can edit other aspects of text say text.color, for example.

Equally could we create a script for our image?

    public Image img;

Could we then use properties of img to alter the image of that object in Unity?

Thom

Yes, but it’s also wider than that, as anything could potentially see it (other scripts for example).

Spot on.

Exactly :slight_smile:

As you progress through the course you will do exactly this, with other objects, the transform for example of an object and so on.

That is one of the best ways to learn in my opinion, as we will all learn things at different rates and so on, but experimenting yourself will help reinforce what you’ve been shown.

Whilst with the text example you could do a lot of this through the Inspector, you could write a little bit of code to alter say the font size, the colour and so on.

/side note: If you change the font size and it gets really big you may find it doesn’t display in your scene, make sure you resize the text object in the scene to accommodate it - I spent a few hours not that long ago wondering why no matter what I did my player’s score wasn’t appearing in the scene… I simply hadn’t made the box big enough to display the extra characters…

/facepalm :slight_smile:

Updated Sat Nov 19 2016 12:18

With regards to the access modifiers, the default within C# is private, so if you see methods without any, you know that that is what they are, for example void Start() / void Update() - just the same as private void Start() / private void Update().

Some further reading on this is here;
https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx

A good rule of thumb is to only to change something from private if you really have to.

2 Likes

Brilliant!

Thanks for all the information, I really appreciate the help!

1 Like

You’re more than welcome Thom :slight_smile:

Privacy & Terms