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