C# : Class Instantiation

Matteo Lo Piccolo
2 min readMay 3, 2021

We make a step back, how we declare a class?
This is a syntax : public, CLASS and the name of the class
In Unity this happens automatically when we create a script, where the name of the script is the name of the class.
Simple.
So, when we create the script, for example, Person, Person is a name of the class.

Person class

Now, we talk a little about the concept of “INSTANTIATE”.
When we create a class, we can DEFINE that class with some variables right?
So, in this class, maybe we need name, age, job. Everything we want.

Now we have a Person class, defined by name, age and job.
Everytime we need this class in other scripts, we have to INSTANTIATE it.

In my Player class, as we see, i take a REFERENCE of Person class.

The Reference to the Person class

This is how we Instantiate Classes.

Now we have access to everything inside Person class (this because I make every variable Public, if we use Private we don’t see nothing).
To try this, go in the Start method, and type

person.

We type person, which is the reference of our class, “dot” to enter in that class and read the data inside it.
As we can see the first thing happer is “AGE”, our variable

TIPS : after we use the dot notation, it opens a menu.

In the lowest part there is a little blue box. If we click that, we see only the FIELDS, so only variables inside.
The Wrench is for PROPERTIES (we don’t see yet), the last icon is for METHODS.

Next step is talk about Custom Class and CONSTRUCTORS!

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!