0:07
In this lecture, we'll learn how we can actually implement
those array ideas from last time in C-sharp.
So let's go do that.
This is one of our games from the previous course when we were processing mouse input.
I'll run the game to show you what it does.
I've started running the game,
and now I've got a character that I can move around,
and when I left click, it changes characters.
What we're going to do in this lecture,
is change the code to use an array
instead of four separate fields for each of the character prefabs.
As you can see, the character changer script has
those four different fields and we're going to replace those with an array.
So let's go to the code and start doing that work.
What we'll do in the character changer script,
is replace these fields with the array.
Now, we're still going to populate the array contents using the inspector.
So we still need to use SerializeField but,
this time, we're going to have an array.
And the syntax we use for the array as we put the element type and,
in this case, that will be game object.
And then, we put square brackets to indicate that this is an array,
and then we give it a name.
And I will call this prefabCharacters, like so.
And now, we have an array of prefabCharacters that we can populate in the inspector.
Now I will say, that it's pretty common to create
the array object at the same time that we declare the array variable.
So I'm going to come back and do that soon.
But first, I want to show you something
interesting between the inspector and this script.
So, we'll go back to the editor,
and as you can see I now have a field called
prefabCharacters and I'll expand to that and it shows that the size is zero.
So this is an array with size zero.
I know I want this array to be size four.
So in the inspector,
I change this zero to a four and hit enter.
And now, I can populate elements zero, one, two and three.
Back in the code though,
this prefabCharacters field doesn't indicate that it actually has four elements.
And I really like to have my code be consistent with what the inspector does.
So I'm going to create an array object here by calling new,
because arrays are in fact objects in C-sharp.
And, I'm going to say game object,
and I put square brackets and I have the semi-colon
and the last thing I need to do is say right here,
how big is this array going to be.
And so, I will give it the size four.
Now, when I populate the array in the inspector.
It will be consistent with the code I see here.
That's just a personal thing.
I hate it when the code doesn't actually show what's really happening,
because the inspector has sort of taken over to say how big the array is.
So I'm going to explicitly include this in the code.
Back in the inspector I need to populate each of
the elements of the array with a prefab before the four different characters.
So I do that in the standard way.
I just come over here and drag and drop.
So I'll do the rest of them and we'll come back.
And now, we can go back to the code and finish making it use the array,
instead of the separate fields.
And I'll do that by deleting the field,
which will give me lots of compilation errors if I F8.
And the first one is here,
because I no longer have a prefabCharacter zero.
The way we access elements of an array,
is we put an open square bracket,
the index of the element we want to access,
and a closed square bracket.
And of course, I also need to change the name to prefabCharacters,
because I regularly name my arrays with a plural,
because they are in fact holding multiple elements.
So you should go do an in-video quiz about this terminology.
And of course, the answer is we access them using the array name and the index,
including those square brackets.
We still have compilation errors and in fact,
here's where you're going to see the big win.
Because we haven't really improved anything,
it would appear at this point.
So let's go down to update
and here's where we actually instantiate a new random character.
And this is the big chunk of code,
we used to do that,
we generated a random number that was zero, one, two,
or three and then based on that number we used an if statement,
to instantiate the appropriate prefab.
I can use the array to make this much smaller by doing the
following: I'm going to cut one of these chunks of code and bring it up here.
And I don't have separate fields for each prefabCharacters.
So, I'll say prefabCharacters and I'll put the square brackets.
And now I know I'm going to run off the right hand side,
so I'll hit enter there.
And now, the index needs to be a random number that zero,
one, two, or three and that's what this code does.
So I can plug it in there.
And at that point,
I can get rid of all the rest of this code.
So, that's where we get a really big benefit from using arrays.
Because I replaced all that if,
else if, else if,
else stuff with this.
And when I run the game now,
you can see that it still changes characters appropriately.
But now, we're using an array to do it and we have reduced the code significantly.
I will reiterate that I don't really like
this inspector inconsistency with what might be in my source code.
So, I'm going to do this a different way so that we don't actually populate
the array using the inspector that we actually do it as we're running the game.
I need to make a few changes to do that.
First of all, over here in the project window,
I actually need a separate folder and I will right click and create a folder,
and this folder needs to be named Resources with a capital R,
it has to be exactly that.
And now, I can put my prefabs into that folder like so.
So as you can see,
I now have a Resources folder with the prefabs folder with those four prefabs in it.
We'll see when we look at the code why I needed to do it that way.
So let's look at the code now.
Because I'm no longer populating the array in the inspector.
I can get rid of this serialized field attribute.
But now, I need to populate the array and I will do that in the Start method.
7:47
So here's how I will do it.
I will say, remember I've already created the array object up here.
So now, I can set
prefabCharacters zero equal to the prefab for Character Zero.
And the way I can do this,
because I put them in that Resources folder,
is I can say Resources.Load and I
put the name of the thing I want to load here and that prefab is called Character Zero.
And I can do that for all four of the prefabs.
When I compile, I see that I actually get some compilation errors and that says,
we can't implicitly convert from object to game object.
The Resources.Load method, loads objects not game objects.
But I do know that each of these prefabs is a game object.
So, I can just typecast each of them to game object.
And now, I don't have any compilation errors anymore.
And when I come back to the editor and run the game,
I immediately get an error over here on the left that says,
I'm trying to instantiate a null object.
So my mistake, is that I should have put
these prefabs directly under the Resources folder rather than in the prefabs folder.
And, I can run the game again.
And, you can see that I can click to change
the character with the resources that we loaded at runtime,
instead of populating them in the inspector.
And you'll find, as you build more complex games,
that you'll actually want to load assets at runtime,
rather than use the inspector to populate them.
But the big ideas behind this lecture are,
that we converted from using four separate fields to a single array with four elements.
To recap, in this lecture we learned how to actually build and use arrays.
We learned that we need to specify the size of the array object when we create it.
And independent of arrays,
we learned how to load prefabs as
resources rather than populating those prefabs in the inspector.