In the last lecture, we created the game UI, but it's not yet being connected up.
So if we play our game, it doesn't do anything.
So that's going to be our job for this lecture.
And because I know you're up to the task, we're going to start off nice and early with a challenge.
I want you to go ahead and write a new script called UI Display and attach the script to the canvas.
Now, this script should be responsible for updating all of our UI elements.
So our slider and our score text and remember that our health script contains the player's current health,
so you'll need to utilize that and the scorekeeper contains the current score.
So pause the video now and give that a go.
Okay.
Welcome back.
So let's go ahead and create our UI display script over in our scripts folder.
Let's right click create new script and call this UI display.
Let's drag that script over onto our canvas and then go ahead and open that up.
To get us started in here.
Let's remove our comments above, start an update.
And because we're going to be dealing with UI components, we're going to have to add our two using
statements at the top.
We're going to need using Unity Engine UI and using TM pro for our text mesh pro elements.
Then for our variables.
Let's split these with some headers and we'll make the first one elements relating to our health.
And the second one will be elements relating to our score.
Under our first health header, we're going to need two variables.
So let's have a serialized field of type slider and we'll call this the health slider.
So that's the object on our UI, and I'm going to want access to the player's health.
So let's have another serialized field of type health called the Player Health.
And the reason we are attaching this manually rather than finding it in the scene is because all of
our enemies are also going to have health scripts.
So for now, it really is just going to be easier to manually connect it in the hierarchy with those
set up.
Let's go under our score header and add another serialized field of type text mesh pro.
You go and call this the score text and then finally we're going to need the scorekeeper.
So we'll call that the scorekeeper so we can grab our current school.
Now, we could go ahead and make this serialized as well, but since there's only one in the scene,
let's just go and find it in a wake so we can say our scorekeeper equals find object of type scorekeeper.
Now before we start updating these elements in the update method, there's one more piece of setup we
want to do for our health slider, and that is to set up its maximum value.
We could do this in the inspector, but because it's linked to the maximum health of the player, it's
probably a good idea to grab that from the health script when the game starts so that if we have a change,
the player's maximum health during the design stages, the UI can just update itself.
Then down in our update method, let's update our UI.
So first of all, we can take our health slider value and that is going to be equal to our player health
dot get health.
And finally, to update our score text, we can say score, text, text equals our score, keeper dot
get score.
And because that returns an integer rather than a string, we can say to string to convert it.
So with that set up, let's jump over into unity to test things out before we can hit play.
We do need to connect things.
So on our canvas let's scroll down to our UI display.
Let's drag over our health slider, let's drag over our player and let's drag over our score text.
This is currently labeled text temp.
So while we're here, let's also change this to score text to make it a bit more descriptive.
And with all of that hooked up, let's go ahead and hit play.
So if we resize our UI, we can see that it will adjust based on the screen size.
We can see our health diminishing and we've died.
So let's restart very quickly.
And this time, if we actually shoot some things.
When an enemy dies, we gain some score.
But I'm not a massive fan of all of this blank space next to the skull.
So let's see if we can pad this out, make it look a bit more like our pre visualization.
Well, if we jump back over into our script, what we can do is we can pass in some parameters into
our two string method and tell our game to always try and print a set number of characters in quotation
marks.
Let's just give it nine zeros.
And now, as we add score, it will add some leading zeros to the front of our score.
So let's save that up, jump back into unity to see this in action.
And now, rather than a score of zero, we have nine zeros.
And now when we kill some enemies, we'll see that our score increases.
And our score text looks a bit more like those old fashioned arcade led displays.
And personally, I quite like this kind of aesthetic with these sorts of games.
You may be different though, so do feel free to format your score text however you prefer.
And with that, the game scene itself is now pretty much finished.
So congratulations on getting your UI all hooked up and working and I'll see you in the next lecture.