1
00:00:03,710 --> 00:00:07,930
We now have our enemies moving along our path and being destroyed at the end.

2
00:00:07,939 --> 00:00:11,750
But this only works for enemies that already exist in the hierarchy.

3
00:00:11,780 --> 00:00:17,060
So in this lecture, we're going to learn how to instantiate our objects into the scene at runtime.

4
00:00:17,330 --> 00:00:23,150
Now, this is going to take some modification to our wave config file and our Pathfinder script, and

5
00:00:23,150 --> 00:00:26,930
we're also going to be writing a new script for our enemy Spanner.

6
00:00:27,200 --> 00:00:30,420
Let's start off by going into our wave config file.

7
00:00:30,440 --> 00:00:34,810
So let's head into our scripts folder and then open up our wave config.

8
00:00:34,820 --> 00:00:40,880
So because in here we're going to want to store all of the enemies that we're going to be instantiating

9
00:00:40,880 --> 00:00:42,560
as part of our future waves.

10
00:00:42,770 --> 00:00:44,720
So let's create a new variable.

11
00:00:44,750 --> 00:00:50,150
It's going to be a serialized filled, and we're going to use a list of game objects.

12
00:00:50,270 --> 00:00:55,430
So we'll have a list of game objects and we'll call this the enemy prefabs.

13
00:00:56,030 --> 00:01:01,310
And the general idea is that we're going to be looping through this list in our enemy spawn and generating

14
00:01:01,310 --> 00:01:04,040
or instantiating each enemy in turn.

15
00:01:04,040 --> 00:01:08,900
So we're going to need two public getters as well to access the information we need.

16
00:01:08,900 --> 00:01:11,980
And we're going to start off nice and early with a challenge.

17
00:01:11,990 --> 00:01:16,760
I want you to go ahead and create a getter called get enemy count.

18
00:01:16,760 --> 00:01:22,820
And this method is going to return the number or count of the enemies in the enemy prefabs list.

19
00:01:22,850 --> 00:01:28,730
Our second getter we're going to call get enemy prefab and this is going to pass in an index number

20
00:01:28,730 --> 00:01:33,630
and we want this to return the enemy prefab at the given index in the list.

21
00:01:33,650 --> 00:01:36,350
So pause the video now and give that a go.

22
00:01:41,920 --> 00:01:42,200
Okay.

23
00:01:42,310 --> 00:01:43,150
Welcome back.

24
00:01:43,180 --> 00:01:52,540
So let's get on method's return and we're going to start off with our public int get enemy count and

25
00:01:52,540 --> 00:01:55,780
all this needs to do is return the enemy.

26
00:01:55,780 --> 00:01:57,670
Prefabs don't count.

27
00:01:58,420 --> 00:02:01,990
Then for our second getter, this is again going to be public.

28
00:02:01,990 --> 00:02:04,240
It's going to return a game object.

29
00:02:04,240 --> 00:02:08,889
So the game object in our list and we're going to call this get enemy prefab.

30
00:02:10,460 --> 00:02:14,720
We're going to need the index of the game object in our list that we're interested in.

31
00:02:14,720 --> 00:02:17,810
So we're going to pass in an event called Index.

32
00:02:19,230 --> 00:02:26,640
And then to retrieve the item from our list, we can just return the enemy prefabs at the given index.

33
00:02:27,120 --> 00:02:30,270
So with that set up, let's save our script.

34
00:02:30,270 --> 00:02:33,420
And next we need to create a new script called Enemy Spawn.

35
00:02:33,810 --> 00:02:35,610
So let's pop back into unity.

36
00:02:35,610 --> 00:02:41,020
And then in our scripts folder, let's create a new C-sharp script called Enemy Spawn.

37
00:02:42,340 --> 00:02:43,600
And then open that up.

38
00:02:44,050 --> 00:02:49,360
Let's clear off the comment above our start method and get rid of the update method entirely because

39
00:02:49,360 --> 00:02:51,070
we're not going to need that at all.

40
00:02:51,250 --> 00:02:56,650
And now let's get a reference to our wave config file, which is going to store our enemy list.

41
00:02:56,740 --> 00:03:03,970
So let's make this a serialized field of type wave config S0 and call it the current wave.

42
00:03:05,080 --> 00:03:11,080
Then in stock, we're going to call a method that we haven't written yet and we'll call this spawn enemies

43
00:03:12,790 --> 00:03:13,390
underneath.

44
00:03:13,690 --> 00:03:15,400
Let's go ahead and write that method.

45
00:03:19,340 --> 00:03:24,410
And this is going to loop through all of the enemies in our WAV config file and spawn them into the

46
00:03:24,410 --> 00:03:25,040
world.

47
00:03:25,250 --> 00:03:29,840
To create objects at runtime, we need to use a method called instantiate.

48
00:03:31,330 --> 00:03:37,000
And if we look at the tooltip here, we have ten different versions of this instantiate that we can

49
00:03:37,000 --> 00:03:37,570
use.

50
00:03:37,780 --> 00:03:42,250
And the reason there are so many is because we can do various different things while we go ahead and

51
00:03:42,250 --> 00:03:43,750
instantiate our object.

52
00:03:43,750 --> 00:03:48,250
So at its bare minimum, we're just going to instantiate the object and not worry about any of the other

53
00:03:48,250 --> 00:03:49,090
parameters.

54
00:03:49,090 --> 00:03:57,940
And we can do this by taking our current wave dot get enemy prefab and then we can pass it in an index.

55
00:03:58,090 --> 00:04:02,770
For now, we'll just create the first one in our list, so we'll give it an index of zero.

56
00:04:02,800 --> 00:04:08,080
And what this will do in its current state is just create that enemy prefab runtime and whatever the

57
00:04:08,080 --> 00:04:10,780
transform position is of our prefab.

58
00:04:10,930 --> 00:04:12,850
However, this isn't quite what we want.

59
00:04:12,850 --> 00:04:16,839
We want the enemy to be instantiated at the start of the path.

60
00:04:17,019 --> 00:04:22,480
So let's go ahead and add some positional information to our instantiation and we can see from the tooltip

61
00:04:22,480 --> 00:04:25,150
that we're already getting some hints on what to do.

62
00:04:25,330 --> 00:04:31,570
Now our starting way point is again stored within our wave script object so we can say current wave

63
00:04:31,870 --> 00:04:34,030
dot get starting waypoint.

64
00:04:34,060 --> 00:04:39,370
This is going to return a transform so we can then go ahead and grab the position of that transform.

65
00:04:39,760 --> 00:04:45,790
Now this is currently giving us an error because there isn't a version of instantiate that just accepts

66
00:04:45,790 --> 00:04:47,020
these two parameters.

67
00:04:47,020 --> 00:04:51,970
Whenever we include a position, we're also going to have to include a rotation as well.

68
00:04:52,120 --> 00:04:57,250
So to save us going off the end of the page, let's break this up into a few different lines.

69
00:04:57,250 --> 00:05:01,830
And we can see here that the rotation needs to be given as a quotation.

70
00:05:01,840 --> 00:05:04,810
Now, don't worry too much about what quaternions are.

71
00:05:04,810 --> 00:05:09,940
This is just a certain way of representing rotations that unity uses behind the scenes.

72
00:05:10,150 --> 00:05:16,150
And to specify no rotation, we can say Quaternions dot identity.

73
00:05:16,480 --> 00:05:20,110
Well, notice that with this addition the red squiggles all disappear.

74
00:05:20,620 --> 00:05:26,740
Now, before we go ahead and test this, we've currently got a wave config serialized filled in our

75
00:05:26,740 --> 00:05:30,730
enemy spanner and we've also got one in our Pathfinder.

76
00:05:30,730 --> 00:05:33,490
Now these need to be the same on both scripts.

77
00:05:33,490 --> 00:05:38,470
So rather than having them as serialized fields and potentially getting into a state where we've either

78
00:05:38,470 --> 00:05:43,330
forgotten to attach one or we get into a state where we've attached two different wave configs to the

79
00:05:43,330 --> 00:05:46,840
different components, let's instead try and link them together.

80
00:05:47,350 --> 00:05:55,030
So in our enemy spawn, let's use this version of our wave config and pass it over to the Pathfinder.

81
00:05:55,030 --> 00:05:58,990
For this, we're going to need a getter to return the current wave.

82
00:05:58,990 --> 00:06:07,750
So let's create a public method that returns a wave config, so called get current wave and this is

83
00:06:07,750 --> 00:06:09,910
going to return our current wave.

84
00:06:10,630 --> 00:06:17,560
Then over in our path finder we can remove this serialized field to make it completely private, but

85
00:06:17,560 --> 00:06:20,380
we're going to need reference to our enemy spawn.

86
00:06:20,410 --> 00:06:25,690
So let's add a new variable of type enemy spawn and we'll call this the enemy spawn.

87
00:06:27,280 --> 00:06:31,420
Then we need to go ahead and find this object which will exist in our scene.

88
00:06:31,720 --> 00:06:41,590
We can do this in the awake method by saying enemy spawn equals find object of type enemy spawn.

89
00:06:42,760 --> 00:06:48,820
Then we need to say that our wave config here is going to be the same wave config from our enemy spawn.

90
00:06:49,300 --> 00:06:55,510
So down in start above where we're trying to set our waypoints because at this point we've already need

91
00:06:55,510 --> 00:06:57,280
our wave config set up.

92
00:06:57,400 --> 00:07:04,360
We can say that our wave config is equal to the enemy spawn top get current wave.

93
00:07:05,140 --> 00:07:09,850
So a fairly minor change in our pathway into there, but one that should hopefully help us in the long

94
00:07:09,850 --> 00:07:10,270
run.

95
00:07:10,270 --> 00:07:16,180
So with all of our scripts saved, let's head back over into unity in our hierarchy.

96
00:07:16,180 --> 00:07:20,890
Let's right click and create a new empty object called the enemy Spawn.

97
00:07:22,300 --> 00:07:27,070
Let's reset the transform and then add our enemy spawn a component.

98
00:07:27,580 --> 00:07:29,560
This is going to need the current wave.

99
00:07:29,560 --> 00:07:33,700
So in our waves and paths, let's drag over wave zero four now.

100
00:07:34,720 --> 00:07:39,790
And if we go ahead and just look at our enemy, we'll notice that our Pathfinder no longer has that

101
00:07:39,790 --> 00:07:42,160
serialized field for the current wave.

102
00:07:42,190 --> 00:07:45,860
It's now going to be grabbing that directly from this enemy spindle.

103
00:07:45,880 --> 00:07:48,880
We've just added now over on the waves themselves.

104
00:07:48,880 --> 00:07:53,240
So in our script or object, we've got this enemy prefabs list.

105
00:07:53,260 --> 00:07:56,720
This needs to be populated with some enemies to be spawned.

106
00:07:56,740 --> 00:08:02,080
So let's go ahead and head into our prefabs folder and drag over a single enemy for now.

107
00:08:02,110 --> 00:08:03,630
Well, in fact, let's just add a few.

108
00:08:03,640 --> 00:08:05,260
Let's add three in total.

109
00:08:05,290 --> 00:08:09,970
For now, we're only generating the first one in this list and ignoring the others.

110
00:08:09,970 --> 00:08:14,290
But in a moment, we're going to look at what happens if we start looping through this list.

111
00:08:14,910 --> 00:08:19,980
With that all set up, let's go ahead and delete the two enemies that we've got in our scene because

112
00:08:19,980 --> 00:08:25,230
we're now going to be generating them at runtime with that instantiation method to test things, let's

113
00:08:25,230 --> 00:08:26,040
hit play.

114
00:08:27,500 --> 00:08:31,190
And we now have an enemy being created and then destroyed.

115
00:08:32,260 --> 00:08:34,720
Now if we do that again very quickly.

116
00:08:35,419 --> 00:08:36,460
And pause.

117
00:08:36,470 --> 00:08:39,620
We'll say that the enemy wasn't in the same before we hit play.

118
00:08:39,620 --> 00:08:40,770
But the moment we hit play.

119
00:08:40,789 --> 00:08:46,790
This enemy gets generated and it has this clone label at the end, meaning that it's being instantiated

120
00:08:46,790 --> 00:08:47,540
at runtime.

121
00:08:47,990 --> 00:08:53,540
Now, if we have a lot of enemies being instantiated into our game, then our hierarchy could get fairly

122
00:08:53,540 --> 00:08:54,100
messy.

123
00:08:54,110 --> 00:08:58,610
So what we could do is store it inside our enemy spawn.

124
00:08:59,630 --> 00:09:03,290
This would essentially drag it into here so we could collapse it up.

125
00:09:03,560 --> 00:09:07,280
And it doesn't have any real effect on the movement of our objects.

126
00:09:07,670 --> 00:09:11,470
So let's see how we can nest it inside our enemies corner.

127
00:09:11,480 --> 00:09:15,680
And while we're there, we'll look at looping through all of the enemies in our list and creating all

128
00:09:15,680 --> 00:09:16,820
of them into our game.

129
00:09:17,060 --> 00:09:18,890
Back over in our enemy spawn.

130
00:09:18,920 --> 00:09:23,630
Let's look at nesting our instantiated enemies inside of our enemy spawn.

131
00:09:24,080 --> 00:09:28,820
We can also do this within our same instantiate method that we're using here.

132
00:09:28,970 --> 00:09:34,430
So if we add another parameter, we can see that it's asking for a transform for the parent.

133
00:09:34,430 --> 00:09:39,680
Well, we want the parent to be the enemy spawn of itself so we can just take the transform for this

134
00:09:39,680 --> 00:09:40,400
object.

135
00:09:40,550 --> 00:09:45,110
And now, when it's instantiated, it will be put inside of our enemy spawn.

136
00:09:45,650 --> 00:09:50,630
Next, we want to loop through all of the enemies in our list and create them into the world.

137
00:09:50,660 --> 00:09:55,190
And we've done the looping several times now, using four and for each loops.

138
00:09:55,190 --> 00:10:01,580
So have a quick think about which one you think is going to be the most useful in this particular situation.

139
00:10:03,240 --> 00:10:03,580
Okay.

140
00:10:03,600 --> 00:10:05,130
I'm not going to give you too long.

141
00:10:05,160 --> 00:10:08,210
We're actually going to use a four loop for this one.

142
00:10:08,220 --> 00:10:13,710
And the reason is that we need the index to be passed in to our enemy prefab.

143
00:10:13,710 --> 00:10:17,820
So we're going to have to keep track of how many times we've gone through this loop.

144
00:10:18,030 --> 00:10:25,890
So for our loop we can say full int I equals zero because we want to start at the first element in our

145
00:10:25,890 --> 00:10:26,400
list.

146
00:10:26,670 --> 00:10:33,210
We want to loop while AI is less than the current wave dot get enemy count.

147
00:10:33,210 --> 00:10:35,910
So that's why we included that getter earlier on.

148
00:10:36,060 --> 00:10:39,330
And then we want to increment I by one every time.

149
00:10:41,790 --> 00:10:48,450
Then with our loop set up inside our instantiation, we want to change this zero to be our iterator.

150
00:10:49,170 --> 00:10:54,390
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

151
00:10:54,390 --> 00:10:55,800
to to say what we mean.

152
00:10:55,800 --> 00:10:58,890
Let's save our script and then test it in unity.

153
00:10:58,980 --> 00:11:05,580
And if we hit play and then very quickly hit pause, we can see that our enemies are now contained within

154
00:11:05,580 --> 00:11:11,010
our enemy spawn, but also notice that they've all generated at exactly the same moment.

155
00:11:11,010 --> 00:11:14,700
And as it stands, they're all directly on top of one another.

156
00:11:14,700 --> 00:11:20,190
So we are looping through our list and instantiating all three enemies within our config file, but

157
00:11:20,190 --> 00:11:24,660
we really don't want them all spawning at exactly the same time and overlapping one another.

158
00:11:24,660 --> 00:11:28,080
We really want to space these out so that they appear over time.

159
00:11:28,290 --> 00:11:33,540
We've made some fantastic progress so far though with Instantiating our Enemies runtime, and in the

160
00:11:33,540 --> 00:11:36,810
next lecture we're going to look at how to space them out over time.

161
00:11:36,810 --> 00:11:38,220
So I'll see you there.

