1
00:00:03,090 --> 00:00:06,540
In this lecture, we're adding lives and scores so that we run over coins.

2
00:00:06,540 --> 00:00:10,890
Our score goes up and when we plummet to our death drop, it shows how many lives we've got.

3
00:00:10,890 --> 00:00:14,930
And we've just reset our score back to zero and our lives back to three.

4
00:00:14,940 --> 00:00:16,079
Let's get started.

5
00:00:16,200 --> 00:00:17,700
Let's start by creating a canvas.

6
00:00:17,700 --> 00:00:21,270
So right click UI and then we'll find canvas.

7
00:00:21,270 --> 00:00:23,970
And then from within canvas we want to change something.

8
00:00:23,970 --> 00:00:28,590
I'm going to change the canvas scalar to be scale with screen size just in case we make some changes.

9
00:00:28,590 --> 00:00:32,759
And my reference resolution 1920 by 1080, you don't have to follow that.

10
00:00:32,790 --> 00:00:33,870
You can do whatever you want in there.

11
00:00:33,870 --> 00:00:38,550
But I just find this as handy for me when I'm making a game that's mostly going to be for me playing

12
00:00:38,550 --> 00:00:39,660
on my PC.

13
00:00:39,690 --> 00:00:44,540
And then next step under canvas, right click, we're going to create UI and then look for text mesh

14
00:00:44,550 --> 00:00:44,880
pro.

15
00:00:44,910 --> 00:00:46,380
That's the new way of doing font.

16
00:00:46,410 --> 00:00:49,610
You can use text, that's fine, but text mesh pros can be a little bit better.

17
00:00:49,620 --> 00:00:52,850
Import TMP Essentials currently we still need to do that step.

18
00:00:52,860 --> 00:00:56,970
Don't need to do the extras and examples, but if you have any issues later on, maybe come back and

19
00:00:56,970 --> 00:00:58,500
explore that under here.

20
00:00:58,500 --> 00:01:02,100
I'm going to change the text name to be What is this?

21
00:01:02,100 --> 00:01:05,310
This is our live text lives text.

22
00:01:05,459 --> 00:01:11,250
Drag that up into the top left, which is where I want it to be and increase in size, make it 200 will

23
00:01:11,250 --> 00:01:12,510
say it's probably a little bit too big.

24
00:01:12,510 --> 00:01:19,470
But what I'll do is in the text field I put in 99 because that's how many maximum lives I think the

25
00:01:19,470 --> 00:01:21,390
play is only going to have up to nine lines.

26
00:01:21,390 --> 00:01:26,460
But it's good to make sure that if for some reason you get crazy in, the player can end up with 11,

27
00:01:26,460 --> 00:01:27,810
12 or 15 lives.

28
00:01:27,810 --> 00:01:29,430
Then you want to make sure it all fits.

29
00:01:29,430 --> 00:01:32,220
Actually, you know, what we should do is give ourselves a different font.

30
00:01:32,220 --> 00:01:33,420
Let's go through that process.

31
00:01:33,420 --> 00:01:39,180
So I like the font as my place to find fonts and I've just searched around for pixel and I found some

32
00:01:39,180 --> 00:01:41,130
text and I'm downloading that.

33
00:01:41,130 --> 00:01:47,130
If you click to download and then find where it has downloaded, you'll need to unzip that file and

34
00:01:47,130 --> 00:01:50,100
then once you've unzipped, you can go back over into Unity.

35
00:01:50,100 --> 00:01:52,920
I'm going to create myself a new folder in the assets.

36
00:01:52,920 --> 00:01:54,300
So click on folder.

37
00:01:54,330 --> 00:02:00,840
We'll call this fonts, double click in fonts, and then I'm going to find where I've unzipped my font

38
00:02:00,840 --> 00:02:03,390
to drag that on in to Unity.

39
00:02:03,390 --> 00:02:08,340
Now there's one more step we need to do in here, which is to jump up to window fine text mesh, pro

40
00:02:08,430 --> 00:02:16,200
font, asset creator, drag our font up into here blank and then click generate font atlas save and

41
00:02:16,200 --> 00:02:19,110
we're going to save it into our fonts folder that looks good as well.

42
00:02:19,110 --> 00:02:25,560
X Out of that window, click back on Live's text and then drag over our font in to font asset and it

43
00:02:25,560 --> 00:02:27,150
will will look a little bit more pixel three.

44
00:02:27,150 --> 00:02:27,750
That's all right.

45
00:02:27,750 --> 00:02:28,860
It's probably a little bit too big.

46
00:02:28,860 --> 00:02:34,410
So I'm going to change it down to say, 150 and I will anchor this up in the top left of my screen to

47
00:02:34,410 --> 00:02:37,140
know that it's going to be there no matter what's going on in my resolution.

48
00:02:37,140 --> 00:02:39,240
There's a little bit of a padding at the top of my font.

49
00:02:39,240 --> 00:02:42,690
Let's see if there's a setting in here we can change to make it go to the top.

50
00:02:42,690 --> 00:02:44,790
It is currently at the top, not middle, not bottom.

51
00:02:44,790 --> 00:02:48,330
So I guess the font just has a bit of a padding at the top of it, which is a little bit annoying,

52
00:02:48,330 --> 00:02:51,240
but I think that will be high enough up on our screen.

53
00:02:51,240 --> 00:02:53,190
We can always do it a little bit above the screen.

54
00:02:53,190 --> 00:02:54,810
Yeah, we'll do that live on the edge.

55
00:02:54,810 --> 00:02:58,920
Need to have it nice and big so my old man eyes can see it, so it looks fine.

56
00:02:58,920 --> 00:03:03,960
What I'm going to do next is click on live text control D to duplicate that, scroll up to the top,

57
00:03:03,960 --> 00:03:08,970
change the name to B score text and then drag that over to the right hand side.

58
00:03:08,970 --> 00:03:11,280
Make the box a little bit bigger just in case we need it.

59
00:03:11,280 --> 00:03:11,880
Right.

60
00:03:11,880 --> 00:03:13,200
Align this one.

61
00:03:13,200 --> 00:03:13,650
Yep.

62
00:03:13,650 --> 00:03:16,540
And then I'm going to add in a few more numbers.

63
00:03:16,590 --> 00:03:21,630
99999996 digits in there just to make sure it all fits on the screen, which it kind of doesn't.

64
00:03:21,660 --> 00:03:22,560
Is that going to be okay?

65
00:03:22,560 --> 00:03:22,740
Yeah.

66
00:03:22,740 --> 00:03:26,850
If the play is up to hundreds of thousands of their score, you want it to take up a big amount of the

67
00:03:26,850 --> 00:03:27,420
screen.

68
00:03:27,420 --> 00:03:31,590
Let's click on play and see if this is going to work and see what errors I think we're going to get.

69
00:03:31,590 --> 00:03:32,040
Okay.

70
00:03:32,040 --> 00:03:32,460
It works.

71
00:03:32,460 --> 00:03:33,060
It looks good.

72
00:03:33,060 --> 00:03:33,840
I'm happy with that.

73
00:03:33,840 --> 00:03:34,740
We get the errors.

74
00:03:34,740 --> 00:03:39,300
Those errors are because we're using text message pro with a new input system and there's a little bit

75
00:03:39,300 --> 00:03:41,460
of conflict going on in the event system here.

76
00:03:41,460 --> 00:03:42,330
It's not happy with this.

77
00:03:42,330 --> 00:03:47,520
So what I'm going to do is click on the event system and then replace with input system input module

78
00:03:47,520 --> 00:03:51,030
in there and then console clear.

79
00:03:51,060 --> 00:03:55,920
Hopefully my errors now go away because basically we've said get with the Times man, we're living in

80
00:03:55,920 --> 00:03:56,430
the future.

81
00:03:56,430 --> 00:04:00,270
You should be able to work with what we're trying to do here and it's happy, happy with our new input

82
00:04:00,270 --> 00:04:00,840
system.

83
00:04:00,840 --> 00:04:02,820
Now let's get the numbers updating properly.

84
00:04:02,820 --> 00:04:09,180
So I'm going to jump back into my project folder, find scripts and open up game session, because I'd

85
00:04:09,180 --> 00:04:10,620
like to have game session managing.

86
00:04:10,620 --> 00:04:11,580
What's your score?

87
00:04:11,580 --> 00:04:12,900
How many lives do you have?

88
00:04:12,900 --> 00:04:16,350
If you lose a life, then your life and score persists.

89
00:04:16,350 --> 00:04:19,800
It knows what's going on and if you restart the entire game then it restarts.

90
00:04:19,800 --> 00:04:24,450
So I'm going to have all that kind of manage through this game session manager It's a nice way to do

91
00:04:24,450 --> 00:04:24,540
it.

92
00:04:24,540 --> 00:04:28,230
So we'll open up our game session script, give ourselves a couple of fields in here because we need

93
00:04:28,230 --> 00:04:30,930
to be saying, what is the particular text we're interested in?

94
00:04:30,930 --> 00:04:39,420
So serialize field and the variable type is going to be text mesh pro you g ui.

95
00:04:39,420 --> 00:04:41,490
It's a bit of a mouthful, but that's what we need to do.

96
00:04:41,490 --> 00:04:46,470
And this first one will be our lives text semicolon.

97
00:04:46,470 --> 00:04:50,070
And I'm not getting the squiggly line because I think I've broken Visual Studio code again.

98
00:04:50,070 --> 00:04:52,920
This happened to me a little bit recently, but I'll persist with that.

99
00:04:52,920 --> 00:04:58,350
I know that as of yet we're not using the right namespace so jump up into using and I need to look for

100
00:04:58,350 --> 00:05:01,680
tmp rho to mpro.

101
00:05:01,710 --> 00:05:02,490
That is a tap.

102
00:05:02,580 --> 00:05:02,970
To that.

103
00:05:02,970 --> 00:05:05,250
Now I'm going to add a second variable while I'm here.

104
00:05:05,250 --> 00:05:13,110
So I'll copy my live text, paste it down below that, and we will rename this to B score text.

105
00:05:13,350 --> 00:05:16,860
Do this while we're here, score, text, save and let's hook that up.

106
00:05:16,860 --> 00:05:18,900
I often forget this step, so let's do it right now.

107
00:05:18,900 --> 00:05:24,150
Before I forget, we need to go to game session and tell it what these two fields are.

108
00:05:24,150 --> 00:05:25,380
So I'm going to drag lives.

109
00:05:25,380 --> 00:05:29,340
Text from canvas into Live's text and score text into score text.

110
00:05:29,370 --> 00:05:32,160
Isn't it nice when I name things properly and don't mess that up now?

111
00:05:32,160 --> 00:05:38,580
We need a way of saying that this number here, should it become this text field just here underneath

112
00:05:38,580 --> 00:05:42,870
a wake, we'll create a start method or recreate that.

113
00:05:42,870 --> 00:05:48,540
And so the conversion of saying, Hey, this is an integer and that is a string, the way we do that

114
00:05:48,540 --> 00:05:52,110
is by first of all, tapping into the text.

115
00:05:52,110 --> 00:05:59,310
So the lives text and the field we're looking for is text, text, and that's going to equal player

116
00:05:59,310 --> 00:06:07,560
lives to string parentheses and semicolon, turn the integer into a string.

117
00:06:07,560 --> 00:06:09,660
Let's save and see how we went with that.

118
00:06:09,660 --> 00:06:13,860
Back over into Unity, click on play and we can see we have three.

119
00:06:13,860 --> 00:06:14,220
Excellent.

120
00:06:14,220 --> 00:06:15,090
Not yet persisting.

121
00:06:15,090 --> 00:06:17,070
If I die it doesn't go down to two.

122
00:06:17,110 --> 00:06:18,060
Goes back to 99.

123
00:06:18,060 --> 00:06:18,510
That's cool.

124
00:06:18,510 --> 00:06:19,770
Let's keep working on that.

125
00:06:19,770 --> 00:06:24,720
So the next thing we need to do is say, well, under process player death, which has been called when

126
00:06:24,720 --> 00:06:28,110
the player has a mishap, we know that we're doing a take life.

127
00:06:28,110 --> 00:06:31,380
When we take the life, we want the player's lives to go down by one.

128
00:06:31,380 --> 00:06:34,020
And we also need to display that on the screen.

129
00:06:34,020 --> 00:06:37,950
So let's grab our lives, text boink, copy that.

130
00:06:37,950 --> 00:06:41,220
Jump back down to take life and we'll add that line in there.

131
00:06:41,220 --> 00:06:44,760
So once again, it's saying, hey, update this, make sure this is accurate on the screen.

132
00:06:44,760 --> 00:06:46,860
So let's save back over into unity.

133
00:06:46,860 --> 00:06:51,150
Click play again, boom, boom, boom, jump and went down to two, then back to 99.

134
00:06:51,150 --> 00:06:53,370
That's because it's not persisting yet.

135
00:06:53,370 --> 00:07:00,780
We have our Singleton on game session, but we don't have any such singleton on our canvas or our lives

136
00:07:00,780 --> 00:07:01,140
text.

137
00:07:01,140 --> 00:07:05,130
And we could put a singleton, we could create a script to say, Hey, do this and do that.

138
00:07:05,130 --> 00:07:09,870
But there's a nifty thing we can do here, which is grab canvas, drag it underneath game session.

139
00:07:09,870 --> 00:07:10,980
So it's now a child.

140
00:07:10,980 --> 00:07:15,600
That means that the singleton we've got on there that don't destroy on load there can be only one is

141
00:07:15,600 --> 00:07:19,620
going to be applied to all of the children underneath game session, which is really handy.

142
00:07:19,660 --> 00:07:20,400
It's a little plus.

143
00:07:20,400 --> 00:07:23,910
That means we need to click on game session and apply the override.

144
00:07:23,910 --> 00:07:24,810
So apply all.

145
00:07:24,810 --> 00:07:26,520
So that's applied to the prefab.

146
00:07:26,520 --> 00:07:32,520
Now when I click on play, I think we're going to be pleasantly surprised when we jump into the spikes

147
00:07:32,550 --> 00:07:37,020
going, goes down to two, stays at two down to one and what happens at zero?

148
00:07:37,020 --> 00:07:38,760
We go back to three because it restarts.

149
00:07:38,760 --> 00:07:41,880
So mission accomplished with our lives.

150
00:07:41,880 --> 00:07:45,990
And if you haven't guessed already, I'm going to give you a challenge to do the same thing with our

151
00:07:45,990 --> 00:07:46,530
score.

152
00:07:46,530 --> 00:07:51,150
What we need to do is when we pick up a coin, we want to increase our score by a certain amount, say

153
00:07:51,150 --> 00:07:51,930
100 points.

154
00:07:51,930 --> 00:07:57,450
This is going to be a little bit more advanced because you need to think about passing in a variable

155
00:07:57,450 --> 00:08:00,300
that says How many points do we want to increase it to?

156
00:08:00,330 --> 00:08:02,520
It's a bit different to lives where it's always just one.

157
00:08:02,520 --> 00:08:06,900
We want to have the ability to change the number of points for different coins because like I'm saying,

158
00:08:06,900 --> 00:08:10,170
we want the ability to set different coins to different points value.

159
00:08:10,170 --> 00:08:15,630
We want to display that to the screen, obviously, and we want it to persist when the player loses

160
00:08:15,630 --> 00:08:18,660
a life and we want it to return to zero when we start a new game.

161
00:08:18,660 --> 00:08:19,830
So there is your mission.

162
00:08:19,830 --> 00:08:21,030
Jump in, take that on.

163
00:08:21,030 --> 00:08:23,010
I'll see you back here when you've given it your best shot.

164
00:08:27,040 --> 00:08:27,400
Okay.

165
00:08:27,430 --> 00:08:30,310
Let's jump back over into our game session script.

166
00:08:30,310 --> 00:08:35,049
I'm going to add a new variable at the top here, which will be a type serialized field.

167
00:08:35,080 --> 00:08:40,600
It will also be an integer and I'll call that just score I think makes sense and that equals nothing.

168
00:08:40,600 --> 00:08:44,320
Well then initialize it nothing because you start with nothing and then you have to get something.

169
00:08:44,320 --> 00:08:49,600
And likewise down in start we need to say, hey, print that to the screen or show it as a string to

170
00:08:49,600 --> 00:08:49,840
string.

171
00:08:49,850 --> 00:08:55,630
So I'm going to copy my lives, text, paste that in, and instead of it being Live's text, it's going

172
00:08:55,630 --> 00:09:00,400
to be score text and instead of being player lives is going to be just straight up score.

173
00:09:00,400 --> 00:09:06,070
And just as we have a public method called process player death, I'm going to add a public method which

174
00:09:06,070 --> 00:09:07,180
will be all about score.

175
00:09:07,180 --> 00:09:14,050
So public void, no return type, and we'll call this add to score or increase score, add scores,

176
00:09:14,050 --> 00:09:14,500
fine.

177
00:09:14,500 --> 00:09:21,460
And then we need an integer that we're asking for to be passed in and call this points to add the points

178
00:09:21,460 --> 00:09:22,510
to add to the score.

179
00:09:22,510 --> 00:09:26,470
And we start with the score we currently have and then we want to plus equals.

180
00:09:26,470 --> 00:09:31,030
So whatever our score is now plus something to that score, the way we write that is plus equals.

181
00:09:31,030 --> 00:09:32,740
And we're adding what to it?

182
00:09:32,740 --> 00:09:37,360
We're adding the points to add points to add semicolon.

183
00:09:37,360 --> 00:09:42,760
And if we want to be able to see that, we need to do the same step we did up here, which is turning

184
00:09:42,760 --> 00:09:44,950
our score text into a string.

185
00:09:44,950 --> 00:09:51,250
So I'm just copying from my start method and then finding underneath my points to add paste that in.

186
00:09:51,250 --> 00:09:53,680
So add the points and then show it on the screen.

187
00:09:53,680 --> 00:09:55,840
And then we need to call this method.

188
00:09:55,840 --> 00:10:01,210
So I'm going to jump up into my explorer, I'm going to find out what is it here that we're looking

189
00:10:01,210 --> 00:10:02,230
for our coin pick up.

190
00:10:02,230 --> 00:10:03,250
Click on that pop.

191
00:10:03,250 --> 00:10:04,810
The Explorer closed again.

192
00:10:04,810 --> 00:10:11,830
Create myself a new variable so serialize field and this would be of type integer and this will be points

193
00:10:11,830 --> 00:10:14,110
for coin pickup.

194
00:10:14,380 --> 00:10:15,580
How many points do you want to get?

195
00:10:15,580 --> 00:10:17,980
And I'll initialize that at say 100.

196
00:10:17,980 --> 00:10:19,570
I think that's a good clean amount.

197
00:10:19,570 --> 00:10:26,020
And then just as we did in our player movement, at some point we need to increase that score when you

198
00:10:26,020 --> 00:10:27,010
run over a coin.

199
00:10:27,010 --> 00:10:28,270
So I'll pop this at the top here.

200
00:10:28,270 --> 00:10:34,360
I'm going to find object of type and that will be game session parentheses dot.

201
00:10:34,360 --> 00:10:36,970
And then we need to call the method we've just created a moment ago.

202
00:10:36,970 --> 00:10:40,420
Add to score and what do we need to pass in there?

203
00:10:40,420 --> 00:10:42,220
How many points do we need to increase it by?

204
00:10:42,220 --> 00:10:47,200
Well, it's this variable that we've just created points for coin pickup semicolon.

205
00:10:47,590 --> 00:10:53,620
Okay, so we're calling our public method and our public method is saying add to the score by however

206
00:10:53,620 --> 00:10:56,530
much you tell me to add to it and then print that to the screen.

207
00:10:56,530 --> 00:11:00,670
So let's see if this is working and just looking at this level, it's not going to work too well because

208
00:11:00,670 --> 00:11:01,870
I don't have any coins in there.

209
00:11:01,870 --> 00:11:04,060
So let's go and add a coin or two.

210
00:11:04,060 --> 00:11:08,350
I know I said this wasn't a coin level, but let's add a couple in there so we can do our testing.

211
00:11:08,350 --> 00:11:09,160
A couple of coins.

212
00:11:09,200 --> 00:11:10,510
I don't even need to do the jump.

213
00:11:10,510 --> 00:11:11,890
Then we'll click on play.

214
00:11:12,070 --> 00:11:14,200
We'll see what happens when we run over a coin.

215
00:11:14,200 --> 00:11:15,970
Hopefully it starts at zero with our score.

216
00:11:15,970 --> 00:11:20,740
It does, and if we don't, the link makes a noise and we'll win.

217
00:11:20,740 --> 00:11:21,130
Hello.

218
00:11:21,130 --> 00:11:23,050
Quick message here from Rick from the future.

219
00:11:23,050 --> 00:11:27,710
We found some folks who have had a bug with the coin pick up where they're picking it up twice.

220
00:11:27,710 --> 00:11:32,170
So I'm just coming back and adding a little bit of an addition that you can add in here if you're having

221
00:11:32,170 --> 00:11:34,290
that problem of the coin being picked up twice.

222
00:11:34,300 --> 00:11:39,280
Thank you very much to the folks who raised this and to Nina, our wonderful teaching assistant, for

223
00:11:39,280 --> 00:11:41,260
giving me a solution to this.

224
00:11:41,260 --> 00:11:46,210
So pretty easy solution to say if you've picked something up, let's make sure you can't pick it up

225
00:11:46,210 --> 00:11:46,600
again.

226
00:11:46,600 --> 00:11:53,110
We'll add a ball, we'll call this was collected and we'll set that at false to start off with.

227
00:11:53,680 --> 00:12:03,130
And now what we want to do is to say if the other tag is player and what and was collected was collected

228
00:12:03,130 --> 00:12:04,000
is not true.

229
00:12:04,030 --> 00:12:07,390
We can write that by saying exclamation mark was collected.

230
00:12:07,390 --> 00:12:11,530
So and if it wasn't collected is what we're saying, then do all this stuff.

231
00:12:11,530 --> 00:12:17,590
But first of all, we want to say, well, it is now officially was collected equals true.

232
00:12:17,590 --> 00:12:19,030
Yes, we collected it.

233
00:12:19,030 --> 00:12:24,100
So that should solve all your problems saying okay, it's been collected once we can't collect it again

234
00:12:24,100 --> 00:12:25,780
if you still having issues.

235
00:12:25,780 --> 00:12:30,760
One thing that Nina thought you could do is just before you destroy the game object, you could add

236
00:12:30,760 --> 00:12:36,670
this extra line which is game object dot set active is false.

237
00:12:36,670 --> 00:12:41,830
That's another way of saying, Hey, just deactivate this game object, make sure it can't do anything

238
00:12:41,830 --> 00:12:42,100
else.

239
00:12:42,100 --> 00:12:45,900
So this line might not be necessary for you, but just an extra step.

240
00:12:45,910 --> 00:12:51,070
Want to make sure super, super duper got you covered in case you're getting that double coin pick up

241
00:12:51,070 --> 00:12:54,670
issue, which might be triggering two sound effects or might be giving you double points.

242
00:12:54,670 --> 00:12:55,120
So there you go.

243
00:12:55,150 --> 00:12:58,390
There's one solution for that hopefully that helps and works out for you.

244
00:12:58,390 --> 00:12:59,010
So there we go.

245
00:12:59,020 --> 00:12:59,800
Mission accomplished.

246
00:12:59,800 --> 00:13:03,490
The one thing I would like to do, though, is when I die on my coins return.

247
00:13:03,490 --> 00:13:04,390
That's not very cool.

248
00:13:04,390 --> 00:13:06,400
You can just die and get your coins over and over again.

249
00:13:06,400 --> 00:13:12,850
So in the next lecture we'll do that so that the coins behave properly when we die and reloading the

250
00:13:12,850 --> 00:13:13,180
scene.

251
00:13:13,180 --> 00:13:13,900
So good stuff.

252
00:13:13,900 --> 00:13:14,980
I'll see you in the next lecture.

