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
In this video we're implementing score for our games so 3
3
that when we shoot enemies our score goes up, up, up. 4
4
And then when we die we have that same score presented on our game over screen. 5
5
So what I wanna do is start off with a challenge in this video and 6
6
the challenge is to add score to your game. 7
7
Killing enemies increases your score. 8
8
And I wanna give you a reminder of what's required by showing you what 9
9
I've just gone and implemented. 10
10
I've come back in the future, back to the future, back from the past. 11
11
Anyway, I've just recorded it, and I'm recording the start of the video now. 12
12
I wanna show you what I've done so 13
13
that if you want some reminders you know what's going on. 14
14
I've created a game session game object out of the game session script of that. 15
15
Within the game session script I'm doing a couple of things, 16
16
I'm creating a singleton pattern, I'm creating a score variable and 17
17
then three public methods to GetScore, to AddToScore and to ResetGame. 18
18
I'm showing you these very quickly, 19
19
I'm going to go through them in super detail in a moment. 20
20
But I'm showing you if you wanna go and do this challenge yourself, 21
21
I'm also creating a score display which is getting the text component that our score. 22
22
So main canvas that our score text is on as well as the game object of game 23
23
session To be out of called the public method of getting the score. 24
24
And then we are updating on every frame, the score by using the ToString method. 25
25
Finally, when we kill an enemy, we need to have a score value and when we die, 26
26
when the enemy dies, we are calling the AddToScore and passing in scoreValue. 27
27
So that's just a really quick rundown, if you wanted to go and 28
28
do this as a challenge yourself and have some reminders and pointers, 29
29
that is the challenge. 30
30
So jump in, take on that challenge now, give it your best shot. 31
31
And I'll see you here when you're done, we'll go through it together in detail. 32
32
Okay, let's get shifty. 33
33
First thing I'm gonna do is free up a little bit of space in my hierarchy, 34
34
right click Create Empty. 35
35
I'm gonna call this Game Elements, and I'm just gonna shove anything in here that I 36
36
don't need have a look at reset that transform. 37
37
So I'm gonna grab the background star field, star field, shredder shredder. 38
38
Drop those into the game elements, the path and the other path, 39
39
drop those in there. 40
40
That's pretty good, and then just close up game elements, 41
41
drag it up to the very top so it's tucked neatly out of the way. 42
42
Excellent, next I'm gonna right click and Create Empty, create a game session. 43
43
Game session, game object. 44
44
Within the Game Session game object, we're going to create ourselves a script. 45
45
Which I will click on my script folder, right click, click C# script. 46
46
I'm gonna call this, you guessed it, GameSession. 47
47
Okay, and then drag the GameSession script onto, 48
48
when it finishes figuring out that it's alive, okay you are alive, 49
49
great, the game session, script under the Game Session game object. 50
50
And then while we're here for due diligence, 51
51
drag the Game Session game object into our prefers folder so it is now prefer core. 52
52
You might have noticed that my assets folder here has changed a little bit. 53
53
I've just gone and drag my slider from folders down in to lists, 54
54
because that's making it a little bit neater and tidier to see what's going on. 55
55
Okay, we're going to need one more script in this video. 56
56
So script folder, right click, create c sharp. 57
57
I'm gonna call this particular script ScoreDisplay so 58
58
that we can be displaying our score. 59
59
So this is our script building block. 60
60
So next stop I'm gonna add my score UI element to the screen. 61
61
So find the game canvas, right click create a UI text. 62
62
Now, we could do TextMeshPro-Text we could do text we tend to do a lot 63
63
of TextMeshPro, 64
64
cuz I think it gives us a little bit more control looks a little bit crisper. 65
65
But in this video I'm gonna be doing score using just tick so that we've done the way 66
66
so you can see it is wanting to do text you know how to do text. 67
67
So were covering a few different bases, 68
68
but in general you'd probably just do one or the other. 69
69
Okay our text we'll rename to be score text and 70
70
then let's see click, if you click on your game canvas, 71
71
that'll zoom you can see score text on your screen. 72
72
So right size is tiny at the moment. 73
73
So let's click on Score Text, change our font to 100. 74
74
Change the color to be, for now, white until I have a better idea. 75
75
Change from the New Text in the text field to 000000, that's kind of six 0s I think. 76
76
And then w, whoops, not a w there! 77
77
Click on Score Text. 78
78
W tall move it roughly up to the top right of my screen. 79
79
I need to zoom in a little bit. 80
80
I'm gonna click on my T-tool. 81
81
And make it bigger so we can actually see it. 82
82
There it goes, it pops into existence, make it nice and large. 83
83
Right click or right align and I like to center line things. 84
84
Now, change the font so font, select that, whatever you've selected for your game, 85
85
I've got batman FA just here, looks pretty crisp over there, 86
86
I like it gonna move it up just a little bit more and very important, 87
87
click on the anchor and then right click on the anchor. 88
88
So no matter whatever screen resolution and aspect ratio we have our score, 89
89
we anchored up to the top right of your screen. 90
90
Okay, now we have score, it called Score Text, perfect, 91
91
that all looking pretty good. 92
92
Our script we just made, which was the ScoreDisplay. 93
93
We're gonna drag that over onto score. 94
94
Excellent, or you can add component and search for it, either of those ways. 95
95
Now we have our score display on our score. 96
96
Our last thing we're gonna do is change the color just to make it a little bit 97
97
more of a bright blue spacey, fluorescent kind of, there we go, wonderful. 98
98
Okay, so I've got score, 99
99
I've got my scripts, we're ready to jump into the scripts. 100
100
Find our game session, double click on that, open it up, jump into Visual Studio. 101
101
First thing I need to do is create a notion of score. 102
102
So score a little s, score initialize that 0; so you have your score. 103
103
Next, I'm gonna implement a singleton, so that our game session there is only one of 104
104
them and we can reuse our score in our game overscreen because it's a singleton. 105
105
So I'm gonna create Awake, so it's the very very first thing it does 106
106
when it wakes up it will SetUpSingleton(); as a method, 107
107
right click on SetUpSingleton quick actions and refactors generate method or 108
108
you can just type it out yourself if you like typing. 109
109
And then, I'm gonna delete the default line that's in there. 110
110
What do we need to do? 111
111
Well, we need the integer, which is how many game sessions we have already. 112
112
So int numberGameSessions and what does that equal? 113
113
Well, we need to find how many sessions there are. 114
114
So I say FindObjectsOfType and again, I'm gonna do the s a few different times. 115
115
So you're like, there's an s in there, don't forget that. 116
116
That's the most common thing, if this line of code's not going well, 117
117
it's cuz we've forgotten the s. 118
118
It's game objects of type. 119
119
We're looking for GameSession(). 120
120
How do we know how long it is? 121
121
Well, we type in length, okay? 122
122
Now we need to do our if, so 123
123
if (numberGameSessions > 1) curly 124
124
brace space we will Destroy(gameObject); 125
125
else DontDestroyOnLoad(gameObject);. 126
126
Save that looking good except we don't have our colored braces. 127
127
Being a little bit hasty here, 128
128
curly braces that wrap around our code block of DontdestroyOnLoad. 129
129
Okay, that's looking pretty good, 130
130
we've got our single term we're running it on Awake. 131
131
We're calling it on Awake. 132
132
Next up we want to add three public 133
133
methods that we can call from other scripts or other areas and 134
134
we get rid of start, don't need that, also don't need update so clear those out. 135
135
Public method number one is gonna be for getting the score, so 136
136
public int GetScore(). 137
137
Why is it an int I ask you? 138
138
It's an int because we have to return a value, and that value is score, and 139
139
the score is an int, so whenever we call that it will return score which is an int. 140
140
Next public method, public, this will be void, there's no return type. 141
141
We'll call this AddToScore, this is where another class or another moment in 142
142
our game can say, hey, game session, please add something to the score. 143
143
What's the something? 144
144
Well we need to know how many points to add to the score. 145
145
This is where we'll have a requirement in here, we'll have a parameter. 146
146
Which we will make it an int, and we'll look for a score value. 147
147
So if you want to call AddToScore, 148
148
you need to tell me the score value that I'm adding. 149
149
And then within our method, within our code block here we take out current score 150
150
+=, what's it gonna plus equals? 151
151
It's gonna plus equal ScoreValue, so it is taking our current score and 152
152
then adding to it, 153
153
where the scoreValue is that was passed to us by the thing that called add to score. 154
154
We will make one more Public Method while we are here. 155
155
Public V=void ResetGame() and then {. 156
156
What are we doing here? 157
157
Well this is just simply destroying this game session objects. 158
158
So, Destroy(gameObject);. 159
159
Okay, so there's game session all created, looking pretty good, 160
160
no functionality we can see in the game just yet but that is pretty tight. 161
161
Next up we need to find our score text and specifically our score display. 162
162
So we've already opened that I believe, 163
163
score display here we go just nice and empty. 164
164
We're working in UI so we want to start off by 165
165
adding a namespace using UnityEngine.UI. 166
166
And remember if you are using TextMeshPro instead of just the text component, 167
167
you'll need to add the TextMesh Pro namespace in here or 168
168
else things won't go as you're expecting them. 169
169
Now, what do we need to do in our score display? 170
170
First of all, we need to get the component where we want to display the score. 171
171
So if you have a look on your text, 172
172
score text, we have the text component just in here. 173
173
So we need to get this, and then within that we have the text field. 174
174
So there's a lot of texts, I know we've dealt with this in previous sections, 175
175
text.text.text it feels like there's 70 text, but this is the text component and 176
176
this is the text value of the text field that is within that. 177
177
So to get access to that text component we start off by creating ourselves a variable 178
178
to store that in which will be a text type variable called scoreText;. 179
179
And in the scoreText;. 180
180
And then when we start our game, we need to give scoreText a value, 181
181
which is going to be the component. 182
182
So GetComponent<Text>();. 183
183
Okay, so get the component text stored as this scoreText variable, 184
184
which we've declared as a type of text, excellent. 185
185
Next, we need to do similar for finding where the score is coming from and 186
186
we know the score is going to be stored in game session. 187
187
So let's create a type GameSession. 188
188
We'll call this gameSession with a small g, semi-colon. 189
189
Now also on the start when our score display comes into existence, 190
190
we need to go and find the game session. 191
191
So we need to define gameSession that we've just created as a variable as, well, 192
192
we need to define it as this gameSession class. 193
193
So that's a type, we need to 194
194
FindObjectOfType <GameSession>();. 195
195
So we've gone and got ourselves a reference to the component, 196
196
and we've gone and got ourselves a reference to the GameSession. 197
197
Now on update, all we need to do is to say 198
198
scoreText.text=gameSession. 199
199
What's our method that we've created in game session, we're looking for 200
200
get score because we need to know the score so gamesession.GetScore(). 201
201
And then we need to be converting this into a string because we 202
202
have an integer at the moment. 203
203
The score itself is a integer and our text field is a string. 204
204
So to convert out integer into a string we say ToString();. 205
205
Sets are looking pretty good, let's jump over to Unity, 206
206
see if we've got any sort of different behavior going on. 207
207
We should have, yes, it goes from allow placeholder zeros to just one zero. 208
208
And then no score going up, but so far, so good. 209
209
Okay, so how do we make our score go up? 210
210
Where is it happening that our score should go up? 211
211
Well, at the moment, we've only got enemies in our game, that's, 212
212
we shoot them, we get points. 213
213
So let's go on find out enemy script, without enemy script we need to start off 214
214
by creating a concept that the enemies give us points. 215
215
And I've been putting this off for awhile but I can't put it off any longer. 216
216
Let's tidy up our variables so it's tidy in our inspector and 217
217
tidy in our script cause this is a big old blob of stuff. 218
218
So I'm gonna create a [Header("")]. 219
219
What are we gonna say in here. 220
220
Well, first up we've got enemy stats, this looks like. 221
221
We've got some enemy stats. 222
222
I'm gonna go and create a new enemy stat which will be 223
223
SerializeField of integer type that I will call scoreValue. 224
224
That'll be the scoreValue of this particular enemy, and 225
225
that will equal 150;. 226
226
Cool, next up, let's see, copy my header code and then paste 227
227
that above the next blob of code this blob of code is all about shooting, I believe. 228
228
Shooting, we have counter shot projectile, projectile. 229
229
Okay, after my last projectile, a couple of spaces. 230
230
Paste in our header again. 231
231
What are these all about? 232
232
This is all about sound effects. 233
233
Sound effects. 234
234
Okay, so much tardier. 235
235
I'm much happier with that. 236
236
Now, we've created score value. 237
237
Let's go and find where we will make our score increase. 238
238
I think when the enemy dies, that's a good place to increase the score. 239
239
So what are we doing here? 240
240
Let's just go remind ourselves over in game session, 241
241
we've got our AddToScore method. 242
242
How do we call add to score method from another script? 243
243
Well, we say FindObjectOfType, 244
244
we're looking for the GameSession(). 245
245
This is what we call our AddToScore();. 246
246
Semi-colon is not working because, can you guess why not? 247
247
Because we need to give it a value. 248
248
A reminder, over here we need the value of scoreValue, 249
249
you need to tell it how many points you're worth, you can't just let it guess. 250
250
So in here, it's gonna be nice and simple. 251
251
We have our scoreValue and we've named it the same, clever us. 252
252
ScoreValue, so back down into AddToScore. 253
253
Score value blank. 254
254
Okay, let's save all that up. 255
255
I think we have implemented everything. 256
256
Click on play. 257
257
What's gonna go on here? 258
258
We will from zero [INAUDIBLE]. 259
259
Okay, score's going up 300, 600. 260
260
Excellent, success. 261
261
But when we die we get a moment to look at the score but 262
262
there's no real moment on here where I can look at the score, call my friend over. 263
263
Hey, look, I beat your score, whatever it might be, 264
264
so let's have the score display on the screen as well. 265
265
And we've set ourselves up with our game session is a singleton, so 266
266
it will continue. 267
267
It will exist and live into the next screen, 268
268
which is very important, the next scene. 269
269
So I've prefabbed that good stuff. 270
270
The game canvas, here's a little thing. 271
271
I wasn't planning on doing this, but just spur of the moment, I think I will. 272
272
See how we currently have our game canvas prefab, and 273
273
there's only one element here the score text. 274
274
So I'll click on apply, so now our game canvas and 275
275
score text have been updated in terms of the prefab. 276
276
So if we say all this up, jump over into [SOUND] where are we, the scene. 277
277
Game Over scene. 278
278
If I find my prefab and drag the Game Canvas 279
279
into this scene, it adds it in there. 280
280
We can have multiple canvases, 281
281
you can have a whole ton of canvases doing different things. 282
282
That's something that we haven't talked about much because we've 283
283
been putting everything under the one particular canvas but 284
284
you could turn our particular canvas on and off. 285
285
You can move the canvases around. 286
286
So this game canvas, I'm gonna change the position of it, of the score text. 287
287
I'm gonna change the score text position to be, let's see in the middle just there. 288
288
So the score goes form being up in the top right, 289
289
when it's game over you get the score right in the middle of the screen. 290
290
Do a couple of things in here, I will just drag it to the right, and 291
291
drag it to the left so. 292
292
And then center align so I know it's right in the middle. 293
293
And then we need to change the anchor point, 294
294
rather than the anchor point being up in the top right. 295
295
That's gonna be a little bit messy if our resolution or our pixel, 296
296
aspect ratio changes. 297
297
Let's anchor this right into the very middle of our screen. 298
298
Okay, there might be a little bit of tidy up of anchoring our other 299
299
elements in the scene. 300
300
I'm not gonna go through that. 301
301
You know how to do that now in terms of anchoring things into a particular 302
302
position if the aspect ratio is changing, 303
303
but this is all looking pretty cool for now. 304
304
We're getting an error down the bottom, assertion failed. 305
305
I think that might have just been cuz I was playing around and 306
306
moving things around. 307
307
We'll clear it and we'll see if it comes up again and then we'll go and 308
308
dive in and see what needs to happen to fix it. 309
309
But let's go back to start, we'll go back to the very start of our game, 310
310
very start start, click on START menu. 311
311
Okay, so click on START, and then sorry, click on Play, and then START. 312
312
We will [SOUND] start our start. 313
313
Okay, score is going up, excellent, we crash into someone. 314
314
[SOUND] 1050. 315
315
Is 1050 retained? 316
316
Yes, it is, all the way here. 317
317
What happens when we play again, goes to zero, and, okay, so 318
318
our score's not going up. 319
319
Why is that? 320
320
I suspect because we are not properly resetting our level loading. 321
321
So let's have a look at our gamesession in here. 322
322
We've created ResetGame, which was clever of us, but 323
323
we're not actually calling it any way. 324
324
Where would we reset our game? 325
325
I think that's gonna be in level, isn't it? 326
326
When the player says load the game, so load the game scene, 327
327
that's where we need to reset. 328
328
So once again, how do we find this particular public method? 329
329
We will say FindObjectOfType and 330
330
it is type GameSession() and 331
331
we're calling ResetGame();. 332
332
Okay, resetting game, so save this. 333
333
I'll jump back over into Unity, click on play. 334
334
See what we can do with the full loop now if we can, 335
335
then we'll declare this success. 336
336
Click on start, shoot, shoot, shoot. 337
337
Get some points, shoot, shoot, die. 338
338
600 should be retained in here, you can put an extra 339
339
text run here saying you scored or your score is 600, if you like. 340
340
Back to main menu, click on Start, start at 0, 341
341
scores goes up and then [SOUND] back into Game Over. 342
342
One more thing we need to try play again. 343
343
Yeah, and the score does go up. 344
344
Okay, just a quick side note here, 345
345
I had a couple of errors on my screen here, I just went and cleared them, 346
346
played the game through a couple of time and the errors don't come back again. 347
347
So I forgot to mention that before I pause the video, 348
348
but I've cleared out all the errors just in case you're wondering. 349
349
So great work in this lecture, great work with your progress. 350
350
Pretty excited to see score in our game. 351
351
We can implement it in so many more places now. 352
352
We can have it when you shoot an enemy, 353
353
the enemy spawns a whole bunch of little coins that you go pick up. 354
354
Like little pickups for points. 355
355
It could be that you just have super mega point 356
356
things floating down your screen that you can pick up. 357
357
Lots of places you can do it above and beyond I just shoot an enemy and 358
358
I get points. 359
359
So great work with this lecture, great work with your progress, and 360
360
I'll see you in the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.