Welcome back, my fellow game developers.
In this video, we not only
have this bar sitting here looking pretty,
when we get hit,
you'll see that our health goes down.
So now when we get hit another time,
There we go, you can see that we die.
Obviously there is this little problem that
we will talk about not addressed right now,
but we will talk about it later on.
So without any further or do a let's not
waste any more time and let's get started.
Okay, so now we have a slider,
we have the text.
All of these represent the health of the player,
but still, when we take damage,
the health doesn't go down.
So what we're going to do is we're going to be changing
this behavior based on using the UI Manager.
So let's go into the UI Manager and let's get
a reference to both the health slider
and the health text.
Now these will be public.
And why are they public?
Well, because we will be
handling them in a different script,
which is the player's health handled.
So I'm going to create a public Slider,
and it's going to be the health Slider.
And remember, we could not have used
this slider if we did not have the Unity engine Ow.ly.
So as you can see, I commented this out and you
can see that the slider now it does not exist.
So that's continue one,
do we have now a reference to the health slider?
Next, we need a reference to
the text of the health vexed.
And these are very basic.
Now we're going to save this and
remember we have an instance,
a static instance in the awake.
So I'm going to go into the player handler,
health handler right here and then start.
As soon as I said, the current health,
I'm going to access the UI Manager dot instance,
dot health slider dot the max value.
And I'm going to set it as the max health.
And then I'm going to do the same with the UI.
Where is it UI Manager,
the instance, health slider,
and the current value,
which is represented by value,
will be the current health.
There we go. So now when I run the game,
if I go into the doom player and I'm going
to set his max health for,
let's say, Oh, okay,
let's just set the current
open because we are already setting here.
I'm just going to comment this out for the reason of
actually having different max value and current values.
So I'm going to set this, let's say at 20.
So when I run the game,
we should see that this Health Bot
will move. And it didn't.
Because obviously, we can see right here
the object reference not set and
I'll know what the problem is if I double-click in here,
we haven't said the health slider on the UI canvas.
So back in the UI canvas,
obviously we need to set this right here.
So health and the slider,
I'm going to drag the slider and the health flexed.
There we go. Nope, not the text.
Oh, okay, So this is
something that we need to talk about.
So the health facts here is not just any text,
it's a text Mesh Pro,
so we cannot be using, where is it?
Where's the UI Manager? There we go.
So I'm going to move it so we cannot just use the text.
We need to have a text Mesh Pro,
but you can see we don't have the text mesh.
Why? Because before we can use the text Mesh Pro,
we need to have the appropriate namespace.
So using, Let's see,
I believe it's the Unity engine dot txt mesh.
So using the TMP,
there we go, hex mesh Pro.
And now in here we can use the text,
text mesh per row.
And there we go. We save that.
Now back in our game.
And unity, I mean,
we can go ahead and drag the health.
I mean, in the UI canvas we can drag the health down.
What is the problem?
I believe that instead of text Mesh Pro,
this also should be text Mesh pro GUI.
Let's try this again.
Save that now back in our game.
And today, now, I will leave that.
Now, there we go.
Finally, we can add,
so we save that.
We run the game and we should
see that the health, yep, There we go.
So we've set the current health to 20,
V-max Health to 100.
So you can see that it's about 1 fifth of the way.
So we know that this is working.
The next step is to have this be updated every
single time we take a hit or we actually start the game.
So I'm going to,
and your challenge is to update the health UI.
So first of all, create
a method that updates the health UI.
So we are talking about the book,
both the slider and the
Next, then you'll need to
use it in the appropriate places.
So you'll need to think of where can I
use the update of the UI?
Make sure you also access
the text and the, and update it.
And finally, I'll give you a hint of
where you can be using this update UI.
You can use it in the start and when the player gets hit.
So with that out of the way,
pause the video right now and go to the Sharon.
Oh, okay, welcome back.
So I'm going to go into the player health handler.
And in here I'm going to create
a private void, update layer health.
And in here what we're going to do is first of all,
we are going to access the UI Manager dot
instance dot Health text, dot text.
And we're going to set it to the current health.
Then we are going to concatenate it.
So I know you haven't done this yet,
but this way we are concatenating.
So first of all, we take the current health,
we add this dash right here or not
that forward slash, it's called, I believe.
And we are going to set the max health and here also.
So we're going to save that.
But not only this,
we are also going to change the value of the slider.
So in here I'm also going to access
the UI Manager dot instance dot health slider dot value.
And it's going to be equal to the current.
Oops, sorry for that.
The current.
So the current health, yeah, that was it.
Save that.
So now the question is,
where do we actually use this?
So there are two places.
First of all, we can use it in the start and
then we kind of
already use it and the start
because we said the current health,
but we are going to remove this line and we are
going to simply call the Update Player health.
And then every time we take damage in here,
after we remove the amount of damage,
we are going to set this,
but I believe it's much better to sell it
after we check the health of 0, maybe not.
The only thing here that I'm
worried about is having the Health
be negative and seeing the bar going the other way.
That's the only thing that bothers me about this.
So I'm going to go ahead and maybe set it in here.
So after we check if we are less than 0,
this may also not look very pretty.
When we die, we still have a bit of health.
Let's see, You know what?
Let's put it in here.
No worries about that.
Later on we will make this
small tweak it changes where we
turn off also the bar whenever we deactivate the player.
So with that done,
save all of that back in our game.
And I'm going to let c in here.
Let's remove this comment,
save that because we actually now have proper health.
I'm going to go into the dome player and let's see.
Let's remove the current health.
This is just to keep track,
we will make sure that it's private later on.
And what did I want to do?
Because the weapon that the enemy is
throwing at us deals and 50 damage, I believe.
So let's make this 300.
So the max health of the player is 300.
I run the game.
And we should see that the max health,
you can see that we have 300 out of 300.
So I'm going to come close to the enemy.
He says, There we go.
You can see that the health is slowly going down.
And finally the last one, we die.
But you'll notice there is a small error that
appeared and it is coming at us a lot.
So I'm going to stop the game.
I'm going to so now we have to consult when those we
don't want that close one of
them and you can see that there is a problem.
And what is the problem?
First of all, we have the enemy bullet controller
can't find the player.
Second of all, we have, I believe.
Okay, So no, not the problem in here.
Well, the problem is actually that we are still trying to
get a reference of the player even though he's dead.
So we're trying to change the health slide.
But anyways, these are small tweaks that we are
obviously going to later on take care of.
But for now I hope you enjoy it.
I hope you now understand and feel how nice our game is.
So everything is done dynamically.
Everything changes automatically using
the code and our game is really taking shape.
So I hope you enjoy it and I'll
see you in the next video.
Don't forget to of course, commit your changes.