In this lecture, we're adding lives and scores so that we run over coins.
Our score goes up and when we plummet to our death drop, it shows how many lives we've got.
And we've just reset our score back to zero and our lives back to three.
Let's get started.
Let's start by creating a canvas.
So right click UI and then we'll find canvas.
And then from within canvas we want to change something.
I'm going to change the canvas scalar to be scale with screen size just in case we make some changes.
And my reference resolution 1920 by 1080, you don't have to follow that.
You can do whatever you want in there.
But I just find this as handy for me when I'm making a game that's mostly going to be for me playing
on my PC.
And then next step under canvas, right click, we're going to create UI and then look for text mesh
pro.
That's the new way of doing font.
You can use text, that's fine, but text mesh pros can be a little bit better.
Import TMP Essentials currently we still need to do that step.
Don't need to do the extras and examples, but if you have any issues later on, maybe come back and
explore that under here.
I'm going to change the text name to be What is this?
This is our live text lives text.
Drag that up into the top left, which is where I want it to be and increase in size, make it 200 will
say it's probably a little bit too big.
But what I'll do is in the text field I put in 99 because that's how many maximum lives I think the
play is only going to have up to nine lines.
But it's good to make sure that if for some reason you get crazy in, the player can end up with 11,
12 or 15 lives.
Then you want to make sure it all fits.
Actually, you know, what we should do is give ourselves a different font.
Let's go through that process.
So I like the font as my place to find fonts and I've just searched around for pixel and I found some
text and I'm downloading that.
If you click to download and then find where it has downloaded, you'll need to unzip that file and
then once you've unzipped, you can go back over into Unity.
I'm going to create myself a new folder in the assets.
So click on folder.
We'll call this fonts, double click in fonts, and then I'm going to find where I've unzipped my font
to drag that on in to Unity.
Now there's one more step we need to do in here, which is to jump up to window fine text mesh, pro
font, asset creator, drag our font up into here blank and then click generate font atlas save and
we're going to save it into our fonts folder that looks good as well.
X Out of that window, click back on Live's text and then drag over our font in to font asset and it
will will look a little bit more pixel three.
That's all right.
It's probably a little bit too big.
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
know that it's going to be there no matter what's going on in my resolution.
There's a little bit of a padding at the top of my font.
Let's see if there's a setting in here we can change to make it go to the top.
It is currently at the top, not middle, not bottom.
So I guess the font just has a bit of a padding at the top of it, which is a little bit annoying,
but I think that will be high enough up on our screen.
We can always do it a little bit above the screen.
Yeah, we'll do that live on the edge.
Need to have it nice and big so my old man eyes can see it, so it looks fine.
What I'm going to do next is click on live text control D to duplicate that, scroll up to the top,
change the name to B score text and then drag that over to the right hand side.
Make the box a little bit bigger just in case we need it.
Right.
Align this one.
Yep.
And then I'm going to add in a few more numbers.
99999996 digits in there just to make sure it all fits on the screen, which it kind of doesn't.
Is that going to be okay?
Yeah.
If the play is up to hundreds of thousands of their score, you want it to take up a big amount of the
screen.
Let's click on play and see if this is going to work and see what errors I think we're going to get.
Okay.
It works.
It looks good.
I'm happy with that.
We get the errors.
Those errors are because we're using text message pro with a new input system and there's a little bit
of conflict going on in the event system here.
It's not happy with this.
So what I'm going to do is click on the event system and then replace with input system input module
in there and then console clear.
Hopefully my errors now go away because basically we've said get with the Times man, we're living in
the future.
You should be able to work with what we're trying to do here and it's happy, happy with our new input
system.
Now let's get the numbers updating properly.
So I'm going to jump back into my project folder, find scripts and open up game session, because I'd
like to have game session managing.
What's your score?
How many lives do you have?
If you lose a life, then your life and score persists.
It knows what's going on and if you restart the entire game then it restarts.
So I'm going to have all that kind of manage through this game session manager It's a nice way to do
it.
So we'll open up our game session script, give ourselves a couple of fields in here because we need
to be saying, what is the particular text we're interested in?
So serialize field and the variable type is going to be text mesh pro you g ui.
It's a bit of a mouthful, but that's what we need to do.
And this first one will be our lives text semicolon.
And I'm not getting the squiggly line because I think I've broken Visual Studio code again.
This happened to me a little bit recently, but I'll persist with that.
I know that as of yet we're not using the right namespace so jump up into using and I need to look for
tmp rho to mpro.
That is a tap.
To that.
Now I'm going to add a second variable while I'm here.
So I'll copy my live text, paste it down below that, and we will rename this to B score text.
Do this while we're here, score, text, save and let's hook that up.
I often forget this step, so let's do it right now.
Before I forget, we need to go to game session and tell it what these two fields are.
So I'm going to drag lives.
Text from canvas into Live's text and score text into score text.
Isn't it nice when I name things properly and don't mess that up now?
We need a way of saying that this number here, should it become this text field just here underneath
a wake, we'll create a start method or recreate that.
And so the conversion of saying, Hey, this is an integer and that is a string, the way we do that
is by first of all, tapping into the text.
So the lives text and the field we're looking for is text, text, and that's going to equal player
lives to string parentheses and semicolon, turn the integer into a string.
Let's save and see how we went with that.
Back over into Unity, click on play and we can see we have three.
Excellent.
Not yet persisting.
If I die it doesn't go down to two.
Goes back to 99.
That's cool.
Let's keep working on that.
So the next thing we need to do is say, well, under process player death, which has been called when
the player has a mishap, we know that we're doing a take life.
When we take the life, we want the player's lives to go down by one.
And we also need to display that on the screen.
So let's grab our lives, text boink, copy that.
Jump back down to take life and we'll add that line in there.
So once again, it's saying, hey, update this, make sure this is accurate on the screen.
So let's save back over into unity.
Click play again, boom, boom, boom, jump and went down to two, then back to 99.
That's because it's not persisting yet.
We have our Singleton on game session, but we don't have any such singleton on our canvas or our lives
text.
And we could put a singleton, we could create a script to say, Hey, do this and do that.
But there's a nifty thing we can do here, which is grab canvas, drag it underneath game session.
So it's now a child.
That means that the singleton we've got on there that don't destroy on load there can be only one is
going to be applied to all of the children underneath game session, which is really handy.
It's a little plus.
That means we need to click on game session and apply the override.
So apply all.
So that's applied to the prefab.
Now when I click on play, I think we're going to be pleasantly surprised when we jump into the spikes
going, goes down to two, stays at two down to one and what happens at zero?
We go back to three because it restarts.
So mission accomplished with our lives.
And if you haven't guessed already, I'm going to give you a challenge to do the same thing with our
score.
What we need to do is when we pick up a coin, we want to increase our score by a certain amount, say
100 points.
This is going to be a little bit more advanced because you need to think about passing in a variable
that says How many points do we want to increase it to?
It's a bit different to lives where it's always just one.
We want to have the ability to change the number of points for different coins because like I'm saying,
we want the ability to set different coins to different points value.
We want to display that to the screen, obviously, and we want it to persist when the player loses
a life and we want it to return to zero when we start a new game.
So there is your mission.
Jump in, take that on.
I'll see you back here when you've given it your best shot.
Okay.
Let's jump back over into our game session script.
I'm going to add a new variable at the top here, which will be a type serialized field.
It will also be an integer and I'll call that just score I think makes sense and that equals nothing.
Well then initialize it nothing because you start with nothing and then you have to get something.
And likewise down in start we need to say, hey, print that to the screen or show it as a string to
string.
So I'm going to copy my lives, text, paste that in, and instead of it being Live's text, it's going
to be score text and instead of being player lives is going to be just straight up score.
And just as we have a public method called process player death, I'm going to add a public method which
will be all about score.
So public void, no return type, and we'll call this add to score or increase score, add scores,
fine.
And then we need an integer that we're asking for to be passed in and call this points to add the points
to add to the score.
And we start with the score we currently have and then we want to plus equals.
So whatever our score is now plus something to that score, the way we write that is plus equals.
And we're adding what to it?
We're adding the points to add points to add semicolon.
And if we want to be able to see that, we need to do the same step we did up here, which is turning
our score text into a string.
So I'm just copying from my start method and then finding underneath my points to add paste that in.
So add the points and then show it on the screen.
And then we need to call this method.
So I'm going to jump up into my explorer, I'm going to find out what is it here that we're looking
for our coin pick up.
Click on that pop.
The Explorer closed again.
Create myself a new variable so serialize field and this would be of type integer and this will be points
for coin pickup.
How many points do you want to get?
And I'll initialize that at say 100.
I think that's a good clean amount.
And then just as we did in our player movement, at some point we need to increase that score when you
run over a coin.
So I'll pop this at the top here.
I'm going to find object of type and that will be game session parentheses dot.
And then we need to call the method we've just created a moment ago.
Add to score and what do we need to pass in there?
How many points do we need to increase it by?
Well, it's this variable that we've just created points for coin pickup semicolon.
Okay, so we're calling our public method and our public method is saying add to the score by however
much you tell me to add to it and then print that to the screen.
So let's see if this is working and just looking at this level, it's not going to work too well because
I don't have any coins in there.
So let's go and add a coin or two.
I know I said this wasn't a coin level, but let's add a couple in there so we can do our testing.
A couple of coins.
I don't even need to do the jump.
Then we'll click on play.
We'll see what happens when we run over a coin.
Hopefully it starts at zero with our score.
It does, and if we don't, the link makes a noise and we'll win.
Hello.
Quick message here from Rick from the future.
We found some folks who have had a bug with the coin pick up where they're picking it up twice.
So I'm just coming back and adding a little bit of an addition that you can add in here if you're having
that problem of the coin being picked up twice.
Thank you very much to the folks who raised this and to Nina, our wonderful teaching assistant, for
giving me a solution to this.
So pretty easy solution to say if you've picked something up, let's make sure you can't pick it up
again.
We'll add a ball, we'll call this was collected and we'll set that at false to start off with.
And now what we want to do is to say if the other tag is player and what and was collected was collected
is not true.
We can write that by saying exclamation mark was collected.
So and if it wasn't collected is what we're saying, then do all this stuff.
But first of all, we want to say, well, it is now officially was collected equals true.
Yes, we collected it.
So that should solve all your problems saying okay, it's been collected once we can't collect it again
if you still having issues.
One thing that Nina thought you could do is just before you destroy the game object, you could add
this extra line which is game object dot set active is false.
That's another way of saying, Hey, just deactivate this game object, make sure it can't do anything
else.
So this line might not be necessary for you, but just an extra step.
Want to make sure super, super duper got you covered in case you're getting that double coin pick up
issue, which might be triggering two sound effects or might be giving you double points.
So there you go.
There's one solution for that hopefully that helps and works out for you.
So there we go.
Mission accomplished.
The one thing I would like to do, though, is when I die on my coins return.
That's not very cool.
You can just die and get your coins over and over again.
So in the next lecture we'll do that so that the coins behave properly when we die and reloading the
scene.
So good stuff.
I'll see you in the next lecture.