Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:00,000 --> 00:00:04,088
[MUSIC]
2
2
00:00:04,088 --> 00:00:08,070
I'm sure you've heard the famous saying,
tidy hierarchy, tidy mind.
3
3
00:00:08,070 --> 00:00:12,464
Well, if we look at our hierarchy when
we start to place down defenders,
4
4
00:00:12,464 --> 00:00:14,783
it becomes very untidy, very messy.
5
5
00:00:14,783 --> 00:00:17,936
You can see all these gnomes and
axes, all sorts of stuff going on,
6
6
00:00:17,936 --> 00:00:21,597
which makes it difficult if we need to
go and figure something out mid-game,
7
7
00:00:21,597 --> 00:00:23,550
pause it and find what's going on.
8
8
00:00:23,550 --> 00:00:25,120
Just much better to have it tidy,
9
9
00:00:25,120 --> 00:00:28,880
particularly when you start making
massive projects, which I know you will.
10
10
00:00:28,880 --> 00:00:33,640
So I'm going to jump over into
my defender spawner script.
11
11
00:00:33,640 --> 00:00:38,550
And up at the very top,
gonna create a game object, gameObject,
12
12
00:00:38,550 --> 00:00:43,060
that will be called defenderParent.
13
13
00:00:45,700 --> 00:00:48,060
Now I need to put back in my start method.
14
14
00:00:48,060 --> 00:00:48,760
So start.
15
15
00:00:48,760 --> 00:00:50,200
What do I wanna have happen on start?
16
16
00:00:50,200 --> 00:00:52,870
Well, I wanna call a method that
17
17
00:00:52,870 --> 00:00:57,100
will take care of creating the defender
child parent relationship.
18
18
00:00:57,100 --> 00:01:01,630
So I'll call this CreateDefenderParent.
19
19
00:01:01,630 --> 00:01:03,705
And parentheses, semicolon.
20
20
00:01:03,705 --> 00:01:09,150
Right-click, quick actions and refactors,
to create that as a method just below it.
21
21
00:01:09,150 --> 00:01:10,770
And what will be in here?
22
22
00:01:10,770 --> 00:01:19,340
Well, first step, I'm going to define my
defenderParent object as GameObject.find.
23
23
00:01:19,340 --> 00:01:20,697
So I want to find something.
24
24
00:01:20,697 --> 00:01:21,504
What am I finding?
25
25
00:01:21,504 --> 00:01:25,130
Well, I'm finding defenders.
26
26
00:01:25,130 --> 00:01:27,020
Now, string references we don't like.
27
27
00:01:27,020 --> 00:01:28,860
They're messy, they're dangerous.
28
28
00:01:28,860 --> 00:01:30,290
Did I even spell it correctly?
29
29
00:01:30,290 --> 00:01:32,480
I did on this occasion, yay for me.
30
30
00:01:32,480 --> 00:01:33,560
What's a better way to do this?
31
31
00:01:33,560 --> 00:01:35,390
Well, we've just been
learning about constant.
32
32
00:01:35,390 --> 00:01:41,630
So at the top of our script let's add
ourselves a const, which will be a string.
33
33
00:01:41,630 --> 00:01:45,155
And we'll use capitals to
show that it's a const,
34
34
00:01:45,155 --> 00:01:48,740
DEFENDER_PARENT_NAME, and
that will equal what?
35
35
00:01:48,740 --> 00:01:51,022
It will equal Defenders.
36
36
00:01:51,022 --> 00:01:54,150
So then we've added our string
reference just once, and
37
37
00:01:54,150 --> 00:01:58,582
then in the future we will have this nice
little hook that we can access when we're
38
38
00:01:58,582 --> 00:02:00,223
typing in our autocomplete.
39
39
00:02:00,223 --> 00:02:02,170
So down here, what are we looking for?
40
40
00:02:02,170 --> 00:02:05,750
Find, in parenthesis,
I'm finding Defender,
41
41
00:02:05,750 --> 00:02:11,340
and you can see it's got
DEFENDER_PARENT_NAME.
42
42
00:02:11,340 --> 00:02:14,420
So it's exactly the same, I could put
my string reference down in there, or
43
43
00:02:14,420 --> 00:02:16,240
I could just do it once and define it.
44
44
00:02:16,240 --> 00:02:22,230
Now what if Defender, so this Defenders
game object, what if it doesn't exist?
45
45
00:02:22,230 --> 00:02:25,290
What if there's no such thing in here or
what if it's called something different?
46
46
00:02:25,290 --> 00:02:27,954
Then we don't wanna be having all
sorts of errors and exceptions.
47
47
00:02:27,954 --> 00:02:31,467
So let's say, so if (!defenderParent), so
48
48
00:02:31,467 --> 00:02:37,480
if the defenderParent doesn't exist, if
there is none, then what do we wanna do?
49
49
00:02:39,570 --> 00:02:45,626
We want to have defenderParent be defined
as, so defenderParent equals new,
50
50
00:02:45,626 --> 00:02:50,874
creating a new, GameObject,
and then DEFENDER_PARENT_NAME.
51
51
00:02:53,074 --> 00:02:55,660
See how nice that was
to have auto completed?
52
52
00:02:55,660 --> 00:02:58,690
Okay, let's save that up,
and, down the bottom,
53
53
00:02:58,690 --> 00:03:00,850
we actually need to use
this one we instantiated.
54
54
00:03:00,850 --> 00:03:03,670
So down the very bottom,
I've got SpawnDefender.
55
55
00:03:03,670 --> 00:03:07,861
And we're creating a new defender, so
56
56
00:03:07,861 --> 00:03:16,090
I will write newDefender.transform.parent
is going to be equal to what?
57
57
00:03:16,090 --> 00:03:20,650
It's going to be equal to
58
58
00:03:20,650 --> 00:03:26,957
defenderParent.transform;.
59
59
00:03:26,957 --> 00:03:33,400
So that means that it's instantiated
as a child to our defenderParent.
60
60
00:03:33,400 --> 00:03:36,360
Save that up, make sure it works,
click on Play.
61
61
00:03:36,360 --> 00:03:41,415
When I drop down my gnomes, get down
some gnomes, [SOUND] You can see that
62
62
00:03:41,415 --> 00:03:47,582
they are created within our, now pause,
cuz we've still got out projectiles there.
63
63
00:03:47,582 --> 00:03:50,536
In our Defenders,
you can see all of my gnomes, and
64
64
00:03:50,536 --> 00:03:53,580
within our defender
gameObjects which is awesome.
65
65
00:03:53,580 --> 00:03:55,866
Okay, and because we do have
all those messy projectiles,
66
66
00:03:55,866 --> 00:03:58,715
we need to take care of those and
that's going to be the challenge for you.
67
67
00:03:58,715 --> 00:04:00,787
So do the same with projectiles,
68
68
00:04:00,787 --> 00:04:04,590
have our projectiles parented
to a projectile game object.
69
69
00:04:04,590 --> 00:04:06,243
Take that on, and
I'll see you back on when you're done.
70
70
00:04:08,872 --> 00:04:10,790
Okay, where do we find this?
71
71
00:04:10,790 --> 00:04:12,460
What's the script we're looking in?
72
72
00:04:12,460 --> 00:04:14,410
Well, I'm going to jump in here.
73
73
00:04:14,410 --> 00:04:17,270
It's gonna be one of two, it's either
gonna be Projectile or Shooter.
74
74
00:04:17,270 --> 00:04:21,040
I'm gonna have a little bit of quick
glance at it, which of these, okay,
75
75
00:04:21,040 --> 00:04:23,980
I'm instantiating on fire,
so I know that it's shooter.
76
76
00:04:23,980 --> 00:04:28,666
Open that up, same process,
up at the top I'm gonna create
77
77
00:04:28,666 --> 00:04:33,280
myself a game object which I
will call projectileParent.
78
78
00:04:35,150 --> 00:04:39,072
Within start we're gonna call
the method weren't created yet
79
79
00:04:39,072 --> 00:04:41,669
which will be CreateProjectileParent.
80
80
00:04:41,669 --> 00:04:48,590
Right-click, Quick actions and Refactors,
generate that method just down below here.
81
81
00:04:48,590 --> 00:04:49,767
What do we need to do?
82
82
00:04:49,767 --> 00:04:55,750
Well, projectileParent is going
to be equal GameObject.Find.
83
83
00:04:55,750 --> 00:04:57,070
What are we finding?
84
84
00:04:57,070 --> 00:04:58,870
Well, we haven't created
our const just yet.
85
85
00:04:58,870 --> 00:05:00,310
So, let's do that at the top.
86
86
00:05:00,310 --> 00:05:06,182
Const string, and
this will be PROJECTILE PARENT,
87
87
00:05:06,182 --> 00:05:09,330
oops, _NAME.
88
88
00:05:09,330 --> 00:05:16,780
That's gonna be equal to or
assigned as Projectiles.
89
89
00:05:17,950 --> 00:05:19,930
Semicolon, okay, so
now what are we finding?
90
90
00:05:19,930 --> 00:05:25,510
We're finding PROJECTILE_PARENT_NAME,
excellent.
91
91
00:05:25,510 --> 00:05:30,280
Same as before, if there is none,
so if parentheses
92
92
00:05:31,310 --> 00:05:36,670
exclamation mark, there's no projectile
parent, then what do we wanna do?
93
93
00:05:36,670 --> 00:05:38,640
Well, we better go ahead and create one.
94
94
00:05:38,640 --> 00:05:45,474
ProjectileParent = new GameObject,
95
95
00:05:45,474 --> 00:05:52,940
in parentheses PROJECT_PARENT_NAME.
96
96
00:05:52,940 --> 00:05:54,270
And you know what?
97
97
00:05:54,270 --> 00:05:55,960
I didn't do this deliberately,
but I've gone and
98
98
00:05:55,960 --> 00:05:58,590
made a mistake that I'll show
you how awesome this is.
99
99
00:05:58,590 --> 00:06:02,240
I've named it PROJECT_PARENT_NAME,
that makes absolutely no sense,
100
100
00:06:02,240 --> 00:06:02,960
what was I thinking?
101
101
00:06:02,960 --> 00:06:04,650
So double-click on that.
102
102
00:06:04,650 --> 00:06:08,713
Control RR, I can see there's three
instances and I can go in here and
103
103
00:06:08,713 --> 00:06:10,780
say PROJECTILE_PARENT_NAME.
104
104
00:06:10,780 --> 00:06:14,540
Apply, there we go, and
we've easily renamed that.
105
105
00:06:14,540 --> 00:06:17,910
Thank you for
having that as a const worked out nicely.
106
106
00:06:17,910 --> 00:06:20,931
And the of course we scroll all
the way down to the bottom and
107
107
00:06:20,931 --> 00:06:24,703
we find where we are instantiating and
we need to add an extra bit in here.
108
108
00:06:24,703 --> 00:06:28,420
Well, first of all, we're only
instantiating this as an object.
109
109
00:06:28,420 --> 00:06:30,740
We need to instantiate as a game object.
110
110
00:06:30,740 --> 00:06:34,950
So what I mean by that, in front
of instantiate we type GameObject,
111
111
00:06:36,480 --> 00:06:38,230
and then we give it a new name.
112
112
00:06:38,230 --> 00:06:45,545
So newProjectile = it's going to be a big
line, but we'll put it on multiple lines,
113
113
00:06:45,545 --> 00:06:50,828
=, gonna give this a new line,
Instantiate(projectile,
114
114
00:06:50,828 --> 00:06:55,970
and the position and
the rotation, and as GameObject.
115
115
00:06:55,970 --> 00:06:56,670
Why do we do that?
116
116
00:06:56,670 --> 00:06:59,120
Well, that's we're casting it,
that's what this is called,
117
117
00:06:59,120 --> 00:07:02,430
and we say as game object we
need this as a game object so
118
118
00:07:02,430 --> 00:07:05,650
that we can place it within our
hierarchy and we can manipulate it.
119
119
00:07:05,650 --> 00:07:10,427
When we instantiate not as a GameObject,
it's instantiated just as an object.
120
120
00:07:10,427 --> 00:07:13,699
And you can see here,
UnityEngine.Object.Instantiate, and
121
121
00:07:13,699 --> 00:07:15,670
we don't have as much control over it.
122
122
00:07:15,670 --> 00:07:20,070
A GameObject allows us to do things that
if it's just an object, we can't do.
123
123
00:07:20,070 --> 00:07:24,613
Okay, it's a very simple explanation but
basically,
124
124
00:07:24,613 --> 00:07:30,701
we're casting it as a GameObject, so
that we can now do more stuff with it,
125
125
00:07:30,701 --> 00:07:33,812
which is going to say newProjectile.
126
126
00:07:33,812 --> 00:07:38,835
NewProjectile, if I can spell
it correctly .transform.parent
127
127
00:07:38,835 --> 00:07:42,619
is going to be =
projectileParent.transform.
128
128
00:07:42,619 --> 00:07:44,712
Save that, let's see how we go.
129
129
00:07:44,712 --> 00:07:47,130
Back over into Unity, click on Play.
130
130
00:07:48,240 --> 00:07:51,591
Drop down some cacti to make sure it's
not just the gnomes that it works with.
131
131
00:07:51,591 --> 00:07:55,074
[SOUND] Okay,
they're shooting a bunch of stuff.
132
132
00:07:55,074 --> 00:07:56,860
We're gonna have explosions that come and
133
133
00:07:56,860 --> 00:08:00,010
go cuz they're being destroyed when
they have done their business.
134
134
00:08:00,010 --> 00:08:02,700
And if we look,
there's a Projectiles game object and
135
135
00:08:02,700 --> 00:08:04,900
we've got some projectiles
that are within that.
136
136
00:08:04,900 --> 00:08:06,620
Excellent so that's mission accomplished.
137
137
00:08:06,620 --> 00:08:07,600
Tidied up the hierarchy.
138
138
00:08:07,600 --> 00:08:09,330
It might seem like a small minor thing but
139
139
00:08:09,330 --> 00:08:11,640
it's really important to
get into these habits.
140
140
00:08:11,640 --> 00:08:14,390
So that you can impress your colleagues
when you're out there working in
141
141
00:08:14,390 --> 00:08:17,230
the game industry with,
look how neat and tidy my hierarchy is.
142
142
00:08:17,230 --> 00:08:18,750
That's one reason of many.
143
143
00:08:18,750 --> 00:08:21,270
So great work, and
I'll see you in the next lecture.
13952
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.