We now have our enemies moving along our path and being destroyed at the end.
But this only works for enemies that already exist in the hierarchy.
So in this lecture, we're going to learn how to instantiate our objects into the scene at runtime.
Now, this is going to take some modification to our wave config file and our Pathfinder script, and
we're also going to be writing a new script for our enemy Spanner.
Let's start off by going into our wave config file.
So let's head into our scripts folder and then open up our wave config.
So because in here we're going to want to store all of the enemies that we're going to be instantiating
as part of our future waves.
So let's create a new variable.
It's going to be a serialized filled, and we're going to use a list of game objects.
So we'll have a list of game objects and we'll call this the enemy prefabs.
And the general idea is that we're going to be looping through this list in our enemy spawn and generating
or instantiating each enemy in turn.
So we're going to need two public getters as well to access the information we need.
And we're going to start off nice and early with a challenge.
I want you to go ahead and create a getter called get enemy count.
And this method is going to return the number or count of the enemies in the enemy prefabs list.
Our second getter we're going to call get enemy prefab and this is going to pass in an index number
and we want this to return the enemy prefab at the given index in the list.
So pause the video now and give that a go.
Okay.
Welcome back.
So let's get on method's return and we're going to start off with our public int get enemy count and
all this needs to do is return the enemy.
Prefabs don't count.
Then for our second getter, this is again going to be public.
It's going to return a game object.
So the game object in our list and we're going to call this get enemy prefab.
We're going to need the index of the game object in our list that we're interested in.
So we're going to pass in an event called Index.
And then to retrieve the item from our list, we can just return the enemy prefabs at the given index.
So with that set up, let's save our script.
And next we need to create a new script called Enemy Spawn.
So let's pop back into unity.
And then in our scripts folder, let's create a new C-sharp script called Enemy Spawn.
And then open that up.
Let's clear off the comment above our start method and get rid of the update method entirely because
we're not going to need that at all.
And now let's get a reference to our wave config file, which is going to store our enemy list.
So let's make this a serialized field of type wave config S0 and call it the current wave.
Then in stock, we're going to call a method that we haven't written yet and we'll call this spawn enemies
underneath.
Let's go ahead and write that method.
And this is going to loop through all of the enemies in our WAV config file and spawn them into the
world.
To create objects at runtime, we need to use a method called instantiate.
And if we look at the tooltip here, we have ten different versions of this instantiate that we can
use.
And the reason there are so many is because we can do various different things while we go ahead and
instantiate our object.
So at its bare minimum, we're just going to instantiate the object and not worry about any of the other
parameters.
And we can do this by taking our current wave dot get enemy prefab and then we can pass it in an index.
For now, we'll just create the first one in our list, so we'll give it an index of zero.
And what this will do in its current state is just create that enemy prefab runtime and whatever the
transform position is of our prefab.
However, this isn't quite what we want.
We want the enemy to be instantiated at the start of the path.
So let's go ahead and add some positional information to our instantiation and we can see from the tooltip
that we're already getting some hints on what to do.
Now our starting way point is again stored within our wave script object so we can say current wave
dot get starting waypoint.
This is going to return a transform so we can then go ahead and grab the position of that transform.
Now this is currently giving us an error because there isn't a version of instantiate that just accepts
these two parameters.
Whenever we include a position, we're also going to have to include a rotation as well.
So to save us going off the end of the page, let's break this up into a few different lines.
And we can see here that the rotation needs to be given as a quotation.
Now, don't worry too much about what quaternions are.
This is just a certain way of representing rotations that unity uses behind the scenes.
And to specify no rotation, we can say Quaternions dot identity.
Well, notice that with this addition the red squiggles all disappear.
Now, before we go ahead and test this, we've currently got a wave config serialized filled in our
enemy spanner and we've also got one in our Pathfinder.
Now these need to be the same on both scripts.
So rather than having them as serialized fields and potentially getting into a state where we've either
forgotten to attach one or we get into a state where we've attached two different wave configs to the
different components, let's instead try and link them together.
So in our enemy spawn, let's use this version of our wave config and pass it over to the Pathfinder.
For this, we're going to need a getter to return the current wave.
So let's create a public method that returns a wave config, so called get current wave and this is
going to return our current wave.
Then over in our path finder we can remove this serialized field to make it completely private, but
we're going to need reference to our enemy spawn.
So let's add a new variable of type enemy spawn and we'll call this the enemy spawn.
Then we need to go ahead and find this object which will exist in our scene.
We can do this in the awake method by saying enemy spawn equals find object of type enemy spawn.
Then we need to say that our wave config here is going to be the same wave config from our enemy spawn.
So down in start above where we're trying to set our waypoints because at this point we've already need
our wave config set up.
We can say that our wave config is equal to the enemy spawn top get current wave.
So a fairly minor change in our pathway into there, but one that should hopefully help us in the long
run.
So with all of our scripts saved, let's head back over into unity in our hierarchy.
Let's right click and create a new empty object called the enemy Spawn.
Let's reset the transform and then add our enemy spawn a component.
This is going to need the current wave.
So in our waves and paths, let's drag over wave zero four now.
And if we go ahead and just look at our enemy, we'll notice that our Pathfinder no longer has that
serialized field for the current wave.
It's now going to be grabbing that directly from this enemy spindle.
We've just added now over on the waves themselves.
So in our script or object, we've got this enemy prefabs list.
This needs to be populated with some enemies to be spawned.
So let's go ahead and head into our prefabs folder and drag over a single enemy for now.
Well, in fact, let's just add a few.
Let's add three in total.
For now, we're only generating the first one in this list and ignoring the others.
But in a moment, we're going to look at what happens if we start looping through this list.
With that all set up, let's go ahead and delete the two enemies that we've got in our scene because
we're now going to be generating them at runtime with that instantiation method to test things, let's
hit play.
And we now have an enemy being created and then destroyed.
Now if we do that again very quickly.
And pause.
We'll say that the enemy wasn't in the same before we hit play.
But the moment we hit play.
This enemy gets generated and it has this clone label at the end, meaning that it's being instantiated
at runtime.
Now, if we have a lot of enemies being instantiated into our game, then our hierarchy could get fairly
messy.
So what we could do is store it inside our enemy spawn.
This would essentially drag it into here so we could collapse it up.
And it doesn't have any real effect on the movement of our objects.
So let's see how we can nest it inside our enemies corner.
And while we're there, we'll look at looping through all of the enemies in our list and creating all
of them into our game.
Back over in our enemy spawn.
Let's look at nesting our instantiated enemies inside of our enemy spawn.
We can also do this within our same instantiate method that we're using here.
So if we add another parameter, we can see that it's asking for a transform for the parent.
Well, we want the parent to be the enemy spawn of itself so we can just take the transform for this
object.
And now, when it's instantiated, it will be put inside of our enemy spawn.
Next, we want to loop through all of the enemies in our list and create them into the world.
And we've done the looping several times now, using four and for each loops.
So have a quick think about which one you think is going to be the most useful in this particular situation.
Okay.
I'm not going to give you too long.
We're actually going to use a four loop for this one.
And the reason is that we need the index to be passed in to our enemy prefab.
So we're going to have to keep track of how many times we've gone through this loop.
So for our loop we can say full int I equals zero because we want to start at the first element in our
list.
We want to loop while AI is less than the current wave dot get enemy count.
So that's why we included that getter earlier on.
And then we want to increment I by one every time.
Then with our loop set up inside our instantiation, we want to change this zero to be our iterator.
Now this is going to do the job we've asked of it, but it's not going to work quite how we want it
to to say what we mean.
Let's save our script and then test it in unity.
And if we hit play and then very quickly hit pause, we can see that our enemies are now contained within
our enemy spawn, but also notice that they've all generated at exactly the same moment.
And as it stands, they're all directly on top of one another.
So we are looping through our list and instantiating all three enemies within our config file, but
we really don't want them all spawning at exactly the same time and overlapping one another.
We really want to space these out so that they appear over time.
We've made some fantastic progress so far though with Instantiating our Enemies runtime, and in the
next lecture we're going to look at how to space them out over time.
So I'll see you there.