Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
[MUSIC] 2
2
I'm sure you've heard the famous saying, tidy hierarchy, tidy mind. 3
3
Well, if we look at our hierarchy when we start to place down defenders, 4
4
it becomes very untidy, very messy. 5
5
You can see all these gnomes and axes, all sorts of stuff going on, 6
6
which makes it difficult if we need to go and figure something out mid-game, 7
7
pause it and find what's going on. 8
8
Just much better to have it tidy, 9
9
particularly when you start making massive projects, which I know you will. 10
10
So I'm going to jump over into my defender spawner script. 11
11
And up at the very top, gonna create a game object, gameObject, 12
12
that will be called defenderParent. 13
13
Now I need to put back in my start method. 14
14
So start. 15
15
What do I wanna have happen on start? 16
16
Well, I wanna call a method that 17
17
will take care of creating the defender child parent relationship. 18
18
So I'll call this CreateDefenderParent. 19
19
And parentheses, semicolon. 20
20
Right-click, quick actions and refactors, to create that as a method just below it. 21
21
And what will be in here? 22
22
Well, first step, I'm going to define my defenderParent object as GameObject.find. 23
23
So I want to find something. 24
24
What am I finding? 25
25
Well, I'm finding defenders. 26
26
Now, string references we don't like. 27
27
They're messy, they're dangerous. 28
28
Did I even spell it correctly? 29
29
I did on this occasion, yay for me. 30
30
What's a better way to do this? 31
31
Well, we've just been learning about constant. 32
32
So at the top of our script let's add ourselves a const, which will be a string. 33
33
And we'll use capitals to show that it's a const, 34
34
DEFENDER_PARENT_NAME, and that will equal what? 35
35
It will equal Defenders. 36
36
So then we've added our string reference just once, and 37
37
then in the future we will have this nice little hook that we can access when we're 38
38
typing in our autocomplete. 39
39
So down here, what are we looking for? 40
40
Find, in parenthesis, I'm finding Defender, 41
41
and you can see it's got DEFENDER_PARENT_NAME. 42
42
So it's exactly the same, I could put my string reference down in there, or 43
43
I could just do it once and define it. 44
44
Now what if Defender, so this Defenders game object, what if it doesn't exist? 45
45
What if there's no such thing in here or what if it's called something different? 46
46
Then we don't wanna be having all sorts of errors and exceptions. 47
47
So let's say, so if (!defenderParent), so 48
48
if the defenderParent doesn't exist, if there is none, then what do we wanna do? 49
49
We want to have defenderParent be defined as, so defenderParent equals new, 50
50
creating a new, GameObject, and then DEFENDER_PARENT_NAME. 51
51
See how nice that was to have auto completed? 52
52
Okay, let's save that up, and, down the bottom, 53
53
we actually need to use this one we instantiated. 54
54
So down the very bottom, I've got SpawnDefender. 55
55
And we're creating a new defender, so 56
56
I will write newDefender.transform.parent is going to be equal to what? 57
57
It's going to be equal to 58
58
defenderParent.transform;. 59
59
So that means that it's instantiated as a child to our defenderParent. 60
60
Save that up, make sure it works, click on Play. 61
61
When I drop down my gnomes, get down some gnomes, [SOUND] You can see that 62
62
they are created within our, now pause, cuz we've still got out projectiles there. 63
63
In our Defenders, you can see all of my gnomes, and 64
64
within our defender gameObjects which is awesome. 65
65
Okay, and because we do have all those messy projectiles, 66
66
we need to take care of those and that's going to be the challenge for you. 67
67
So do the same with projectiles, 68
68
have our projectiles parented to a projectile game object. 69
69
Take that on, and I'll see you back on when you're done. 70
70
Okay, where do we find this? 71
71
What's the script we're looking in? 72
72
Well, I'm going to jump in here. 73
73
It's gonna be one of two, it's either gonna be Projectile or Shooter. 74
74
I'm gonna have a little bit of quick glance at it, which of these, okay, 75
75
I'm instantiating on fire, so I know that it's shooter. 76
76
Open that up, same process, up at the top I'm gonna create 77
77
myself a game object which I will call projectileParent. 78
78
Within start we're gonna call the method weren't created yet 79
79
which will be CreateProjectileParent. 80
80
Right-click, Quick actions and Refactors, generate that method just down below here. 81
81
What do we need to do? 82
82
Well, projectileParent is going to be equal GameObject.Find. 83
83
What are we finding? 84
84
Well, we haven't created our const just yet. 85
85
So, let's do that at the top. 86
86
Const string, and this will be PROJECTILE PARENT, 87
87
oops, _NAME. 88
88
That's gonna be equal to or assigned as Projectiles. 89
89
Semicolon, okay, so now what are we finding? 90
90
We're finding PROJECTILE_PARENT_NAME, excellent. 91
91
Same as before, if there is none, so if parentheses 92
92
exclamation mark, there's no projectile parent, then what do we wanna do? 93
93
Well, we better go ahead and create one. 94
94
ProjectileParent = new GameObject, 95
95
in parentheses PROJECT_PARENT_NAME. 96
96
And you know what? 97
97
I didn't do this deliberately, but I've gone and 98
98
made a mistake that I'll show you how awesome this is. 99
99
I've named it PROJECT_PARENT_NAME, that makes absolutely no sense, 100
100
what was I thinking? 101
101
So double-click on that. 102
102
Control RR, I can see there's three instances and I can go in here and 103
103
say PROJECTILE_PARENT_NAME. 104
104
Apply, there we go, and we've easily renamed that. 105
105
Thank you for having that as a const worked out nicely. 106
106
And the of course we scroll all the way down to the bottom and 107
107
we find where we are instantiating and we need to add an extra bit in here. 108
108
Well, first of all, we're only instantiating this as an object. 109
109
We need to instantiate as a game object. 110
110
So what I mean by that, in front of instantiate we type GameObject, 111
111
and then we give it a new name. 112
112
So newProjectile = it's going to be a big line, but we'll put it on multiple lines, 113
113
=, gonna give this a new line, Instantiate(projectile, 114
114
and the position and the rotation, and as GameObject. 115
115
Why do we do that? 116
116
Well, that's we're casting it, that's what this is called, 117
117
and we say as game object we need this as a game object so 118
118
that we can place it within our hierarchy and we can manipulate it. 119
119
When we instantiate not as a GameObject, it's instantiated just as an object. 120
120
And you can see here, UnityEngine.Object.Instantiate, and 121
121
we don't have as much control over it. 122
122
A GameObject allows us to do things that if it's just an object, we can't do. 123
123
Okay, it's a very simple explanation but basically, 124
124
we're casting it as a GameObject, so that we can now do more stuff with it, 125
125
which is going to say newProjectile. 126
126
NewProjectile, if I can spell it correctly .transform.parent 127
127
is going to be = projectileParent.transform. 128
128
Save that, let's see how we go. 129
129
Back over into Unity, click on Play. 130
130
Drop down some cacti to make sure it's not just the gnomes that it works with. 131
131
[SOUND] Okay, they're shooting a bunch of stuff. 132
132
We're gonna have explosions that come and 133
133
go cuz they're being destroyed when they have done their business. 134
134
And if we look, there's a Projectiles game object and 135
135
we've got some projectiles that are within that. 136
136
Excellent so that's mission accomplished. 137
137
Tidied up the hierarchy. 138
138
It might seem like a small minor thing but 139
139
it's really important to get into these habits. 140
140
So that you can impress your colleagues when you're out there working in 141
141
the game industry with, look how neat and tidy my hierarchy is. 142
142
That's one reason of many. 143
143
So great work, and I'll see you in the next lecture.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.