What is the point of public and what does text text mean?

Hello there!

I’am a bit confused on this whole public thing I don’t really see the importance of it and what it actually does. Maybe I’am just not understanding the logic to public or what the teacher is explaining. But from what I’am seeing it acts like a function that can be called by using text.text. Could anyone go more indepth of whats going on? Also is Text text the way to use public classes or can I use for example

public  One two;
one.two = "Hello world";
1 Like

public is what is known as an access modifier, it determines what can see and acess your variables, properties and methods.

By declaring your text variable public you are stating that everything in your project will be able to see/access it and in this specific case it is so that it is available within the Inspector.

Text is a type, it is the type of the UI.Text object you dragged into your scene.

text is the variable that will hold the reference/value given to it.

So, when you drag your UI.Text game object into the Text field within the Inspector you are populating the text variable (note that Unity capitalises the name).

Your example wouldn’t quite work as One isn’t a Type. Your could however do something like this;

public Text story;

In the Inspector you would then see the field Story appear which you could drag the UI.Text game object on to.

story is of type Text

Finally, you can access the text field of the UI.Text game object by using .text, so you might have something like;

public Text story;

story.text = "Hello World";

text.text works the same way but is perhaps a bit confusing because of all of the references to the word “text” with its varying capitalisation and meanings in the example.

3 Likes

Thank you very much for the information! I’am actually a beginner to programming so when you went into details of class/type I got a bit confused on what that means. I actually went back and looked at the Basic for noob guide you guys provided and explained the terms and what they do! :slight_smile:

1 Like

Sorry, I should have explained in a little more detail, I will remember that for the future.

So, when you create a script in Unity it creates a little bit of code for you from a template, just the basics to allow it to run, but not really do anything within your game.

You’ll notice at the top of the code there is a class definition, e.g. public class Smurf for example.

The access modifier is there as mentioned earlier, and you could think of a class as being a template for the creation of objects, we would call these instances, e.g. a specific instance of our class

Properties and behaviours can be added to this class so that they can be used by the instance or other code which uses it.

For example, if I had a class called Square it may have a couple of fields (class variables) which hold width and height values for that square.

It may have a behaviour (method) which calculates and returns the area of that instance of Square, using the width and height fields.

I could potentially use this same Square class to create many different Square instances, all of which may have differing widths and heights.

Every object will have a type, e.g.

string message = "Hello World!";

Message is your variable and it’s a String, so we would say “message is of type String”

int robsAge = 41;

robsAge is the variable and it’s an integer (whole number), we would say “robsAge is of type Int” (and blimey, he’s old! :wink: )

So with your own classes your are creating your own types… in the above example, smurf would be the type. In the shape example, square would be the type.

I hope the above is useful, do say if you have any other questions and thanks for the feedback on the initial reply, I will bear it in mind for future replies :slight_smile:

1 Like

I too, like HanSoloYang, struggle a bit with the technical jargon as a fledlging programmer. Where might I find the referenced “Basics for Noobs Guide” that he referenced above?

I have no idea what the -“Basics for noobs”_ guide is, sorry, I think you would need to ask @HanSoloYang to perhaps provide a link if it is an online reference.

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