- [Computer Scientist] In Unity, a script may
contain a definition for a class.
If we were to extend our metaphor where
variables are boxes and functions are machines,
then classes would be the factories that these
boxes and machines are in.
You may have noticed that scripts from other
tutorials have the keyword class near the top.
Unity automatically puts this in the script for you
when you create a new C# script.
This class shares the same name as the
script file that it is in.
This is very important, because if you change
the name of one, you need to change the name
of the other.
So, try to name your script sensibly
when you create it.
The class is a container for variables
and functions, and provides, among other things,
a nice way to group things together, that work together.
They're an organizational tool, in something
known as Object-Oriented-Programming, or OOP for short.
One of the principles of
Object-Oriented-Programming is to split your
scripts up into multiple scripts, each one
taking a single role or responsibility.
Classes should therefore be dedicated
ideally to one task.
In this example we have a script that
handles a number of different tasks.
So, we should split it up into three
shorter scripts, to make it easier to manage
and work with.
This script, for example, handles an inventory,
movement, and shooting.
Here's the script in action when attached to
a GameObject.
We can drive around, we can shoot
some small objects, and we can also
keep track of items in our inventory.
But, this is all contained within
a single script. What we should be doing
is organizing this into Inventory,
Movement, and Shooting classes.
So, if I switch to another scene, which
looks identical, we see that the object has
scripts which handle each of these things
separately and individually.
This makes these scripts easier to manage,
easier to read, and also more efficient to write.
Let's take a look at a use of a class.
In this example, we have our Inventory class.
And within that, we have a subclass called Stuff.
We create the class here and it contains
three int-variables, one for "projectileA",
one for "projectileB", and one for
"projectileC". We then create an instance
of this class, known as an Object, down here.
We give it the data-type, which is effectively
the name of the class, and then we give it a name,
and we say 'new', and again,
reuse the name of the class.
The brackets on the end of the name of the class
imply that a constructor is being used.
Whenever a class or struct is created,
it's constructer is called.
A class or struct may have multiple constructors,
that take different arguments.
Constructors enable the programmer
to set default values.
Limit instantiation, and write code,
that is flexible and easy to read.
For example, we might set up a constructor.
Here, we've given default values to
each of the int variables in our class.
This means that instead of being intialised as zero,
they'll be given a default value of 1.
What we can also do is write our
own constructor, that allows us
to set parameters for those variables.
So, for example, we might say public Stuff,
and then inside the brackets, create some parameters.
Then, what we might do, is assign our variables
to these parameters.
This means that when we create an object,
or an instance of our class, down here,
we can now use the brackets to define
what those default values will be.
We know that if we put in an integer, then a comma,
we'll be defining exactly what those variables within
this instance of the class will be.
For example, we might have fifty of projectile A,
and you can see the visual studio is helping
me to figure out what I'm doing, telling me
as I write them.
And let's say we've got five of projectile B,
and we've got five of projectile C.
A few things to note about constructors.
First off, the name of the constructor
is always the name of the class.
Constructors never have a return-type,
not even void.
A class can have multiple different constructors,
but only one of them gets called
when an object is intialised.
For example, our first constructor sets up
values for these three integers.
But, what if we had more values.
Let's put in a float for how much
fuel we're carrying.
Now, currently this constructor
does not set up a value for that.
But, we could set up another constructor
to do that job for us.
Using Visual Studio, we can automatically
insert a snippet by first typing C T O R, and
then hitting tab, to insert the snippet.
And then, if we use the parameters set up
in this constructor, what we'll see
is that this constructor will be used
to set up our new object.
So, if I make one that has, for example,
projectile A, and then a float for the fuel.
Now that I have a separate constructor,
what I can do is create an instance,
and use a different constructor based upon
what parameters I put in to that instance.
So I can say New, Stuff, and this time I can
say for example, 50 of projectile A,
and a value of 1.5, a float, for fuel.
Now, this instance of the class is gonna use
this constructor, because the parameters match.
Whereas my other instance of the class will use
this constructor, because those parameters match.
So, you can do a lot of different things
with constructors, and you can have multiple
different constructors for the same class.
Hopefully you can see how useful classes are at
organizing your data when creating games,
and it's recommended that you think through
how your scripts are gonna be laid out
before you go and just write one huge class
that has loads of different things in it.