Let's keep health amount to this enemy pawn game object.
So let's select the enemy one game object in the hierarchy, and let's open up the enemy one script
by double clicking on it.
And let's go to top of our class.
And let's declare a variable for our Maxell.
So we will scroll up and let's make it over here.
Uh, let's make it here.
So just wait and let's give it a title before we create the variable or declare the variable.
So let's give a header header.
Sorry we're going to give a header.
So let's make this square brackets header.
And we're going to say something like health uh health.
And let's create a serialized field.
So let's create a serialized field private.
And this will be int.
And we can call this one something like Max health.
And by default I'm going to give something around three unit of health.
And now we're gonna make a function in order to take damage.
And let's make it in the bottom somewhere over here.
And we're going to make this function public, since we're going to call this function from another
class or another script.
So that's why for so let's mark this function as public.
And for function we're going to type void.
And we have to name the function.
So let's call it something like take damage.
Let's call it take damage.
Let's make first bracket and some curly brackets.
And this take damage function.
Just going to take an argument.
And that will be in type like whole number 01234.
And we can call this one amount a amount something like that.
Let's call it instead damage amount.
So let's call it damage amount.
And later on we're going to replace this damage amount with real numbers like 1234.
But I'm going to test with one.
So now what we basically have to do inside that function.
We basically need to subtract from this max health by that damaged amount.
So let's do that.
So we will subtract from our max.
So max minus equivalent in order to subtract that.
And we just want to subtract with our damage amount.
And let's close that up with semicolon.
So now this finds.
So let's suppose we call this takedamage function for the first time.
So the max health by default we have given three.
And let's assume this damage amount we pass one.
So this one is one.
So three minus one is just going to be two.
And if we call this damage function for the second time max health is now two.
So two minus one is going to be one.
And if we call this takedamage function for the third time max health is now one.
So one minus one is going to be zero.
So our max health just going to hit zero whenever uh at some point it's hit it just going to hit zero.
So in that moment we need to destroy our player game object.
And let's, uh, let's suppose, uh, our max health is already zero.
And let's suppose we call this function.
So the max cell is zero.
So zero minus one gonna be minus one.
So it will move over to negative value.
So before we subtract we want to check if the max cell is already zero.
Then we need to stop subtracting.
So we will check if if let's make first bracket and some curly brackets and what we want to check over
here.
We basically want to check if our max cell, if it's lesser than or equal to zero, means we need to
stop.
Subtracting means we need to stop executing this line of code which is subtracting.
So that's why we're going to type this return.
And after return, whatever we're going to do it's not going to be executed at all means our Maxwell
just going to start at zero.
As you can see, whenever our max cell hits zero, as you can see right over here, we type in return
and after return, whatever we're going to do, it's not going to execute at all means our Maxwell just
going to gonna start at zero.
And we also need to create a die function in order to destroy our player.
So let's make it over here.
And this function doesn't need to be public or private or public.
So we can make it private instead.
So if we type void and if we don't assign a right in front of this, something like private or public,
then this function automatically get marks as a mark marked as a private function.
For function we type void and we have to name the function.
So I'm going to call it die.
Let's make first bracket and some curly brackets.
So what we want to do inside this function we basically want to destroy our enemy one game object.
But before we destroy let's print out the message that we want to see something like this enemy one
game object dies.
So for printing out message we're going to type this debug dot log.
And we can say something like this dot game object dot name, dot name, and top of that I'm going to
add a custom message.
So use this plus icon.
Sorry.
Use this plus and then quotation and you can put whatever you want in between these quotation.
Say something like diet and let's close that up with semicolon.
So this dot game object basically means it just referencing itself.
So the enemy one script we can see it's attached to this anime one game object.
And we can see the name of this anime one in Inspector we can see is anime one.
So the message should be pop up.
So this right over here is just going to be anime one, basically the name of the game object.
And on top of that we just added this message which says type.
So anime one type message should be pop up in our console tab whenever we're going to call this function.
So we can call this function once our Maxell loses zero right over here, we can call it uh, we can
call it or we can call it in the update function as well.
So let's call it inside update function instead of doing.
There.
So let's move over to this update function.
So we do know that update function runs.
Every single frame.
So every single frame we're just going to keep track of our max cell.
So we will check.
If so let's make first bracket and some curly brackets.
So what we want to check.
Over here we basically want to check the max health of that animation.
So if our max health max health.
If it's less than or equal to zero means we need to destroy our plate.
Sorry.
We need to destroy our enemy.
Itself.
So we can simply call the die function.
And what die function just going to do?
It's going.
To print out the message in the console says this dot game Objectname basically the name of the game
object, which is anime one.
And top of that we added this message says type.
So enemy one died.
Message should be pop up in our console tab.
So let's call the function here once our max health reaches zero.
So we're going to call the function ty.
Let's make first bracket and then close that off with semicolon.
And we're going to press down Ctrl s to save our script.
And now let's move over to unity.
So we should be able to notice that message whenever our Maxwell just reaches zero of this enemy one
GameObject.
But the thing is that we're not going to able to notice why we just made that Takedamage function,
and we never calling it anywhere.
So we also need to call it.
But when we need to call it once, these are our player GameObject attack on this enemy one game object.
Once our player GameObject attack on this enemy one GameObject.
In that moment, we need to call that function from that enemy script or from that enemy one C class.
So just wait a moment and you will see.
And a nice title just gonna pop up underneath our enemy one script as says Max health or health as we
have given the title.
And also, I really apologize for taking a little bit longer time than as usual, you know that my system
is a little bit slow, so that's why it's taking pretty much long time.
Uh, so in this time lapse, I can just I can just explain what, uh, basically going on.
And now enemy one script just getting compile, and once it's compiled, we can see underneath our enemy
one script in Inspector Health pop up and the max health by default we have given five.
So if I move over to my console tab and then click on this play button and try to damage this enemy,
one game object, uh, this enemy, one game object, then this enemy one should lose, uh, some unit
of health from this max health.
But the problem is that it own.
Why?
Because we also need to call the Takedamage function.
Let me show you.
So just wait a moment.
So now if we just get in attack range and try to attack on this enemy one.
Uh, that, as you can see, uh, Max health is not going to four, three, two, one as even though
we attacking, uh, even though we attacking.
So what basically going on if I get out of play mode and then move over to my enemy.
One script that we just created this function and we never calling it anywhere.
So that's why this line of code not getting execute basically we not subtracting at all our damage amount.
So when we need to do it, once our uh player GameObject attack on this enemy one game object.
So if I move over to my console tab, as you can see, a message just getting pop up says circle colliding
with enemy.
Basically, if I select my player GameObject in the hierarchy, move over to animation tab and then
choose this attack one or attack two.
As you can see right over here, we created that attack function.
So over here we can call the function which is damage amount.
So basically what we need to do we need to open up our attack function inside this player game object
that we created earlier.
So we will select our player game object in the hierarchy.
Then go over to Inspector and open up the player script by double clicking on it.
And let's move over to that function.
In which function?
We just, uh, we basically, uh, just, uh, printing out the message.
As you can see, if call info is true basically means the circle is colliding.
Basically this circle colliding with this enemy one game object.
Once our this player just playing the attack animation clip.
So instead of printing out message like this, as you can see, uh, our circle collided with and the
name of the game object.
Instead of doing that, we can simply call the Takedamage function from that enemy class as we created
before, so we can simply call it over here.
So instead of printing out message, we're going to call.
But before we call we just want to check.
So what we want to check is the circle.
If the circle is really collided with this enemy one game object, then only that moment we want to
call the Takedamage function.
Basically this enemy one just gonna take damage, else we're not gonna do it.
So before we damage it, we need to check what we colliding with.
So that's why we're going to use this call info in order to extract information.
So we will use this if condition.
Let's make first bracket and some curly brackets.
So what we want to check over here.
We basically want to check if call info dot game GameObject dot get component.
And we will check for our enemy one script.
So we will type enemy one if it's and then make this first bracket.
And we will check if it's not equal to null basically means what.
Basically this means call con, call info.com object basically means the game object the circle collided
with.
So the circle is just going to collide with this enemy one game object.
Then what we're doing we just going over to that.
Then we're just taking our enemy one script.
As you can see Dot get component and enemy one.
And we're checking if that script is not equal to null.
So firstly we're just going over to this enemy one game object and accessing this enemy one script.
And we're checking if this enemy one script is not equal to null basically means that this enemy one
script is attached to this enemy, one game object.
And indeed we can see enemy one script is attached to this enemy, one game object.
So if that is the case and not equal to null, basically means that enemy one is script is attached
to that enemy one game object.
Now we can what we can do, we can simply call our Takedamage function from that enemy one script.
But before we do, let's print out a message now.
Say something like a text message.
So we're going to type this debug dot log in order to print out message.
And we can say any one taking damage something like that.
And upon taking damage damage.
And let's close that up with semicolon.
And we're going to press down Ctrl S to save our script.
And now let's move over to unity.
And the circle really gonna collide with this enemy one.
And we're checking if the enemy one script is not equal to null basically means that enemy one script
attached to this enemy one game object.
And indeed we can see enemy one script is attached to this enemy, one game object.
Then what we're doing, we printing out a message in the console says enemy one taking damage.
Enemy one taking damage.
So this message is just going to pop up whenever we're gonna attack on this enemy one game object once
our player just gonna attack on on to this enemy.
One game object.
So I'm going to select my console tab.
And let's click on this play button in the top in order to play our game.
And let's have a look on this.
And we created the Takedamage function inside this enemy one script.
So we need to call the function.
So if we call that function then this enemy one just going to take damage.
So if I get in attack range and then damage it basically uh attack on it, we can see a message.
So if I pause my game in the top by clicking on this pause button in the top, you can see in the middle
top.
And if we move over to our console tab, we can see a message just getting pop up says enemy one taking
damage.
So instead of printing out message what we really want to do, we basically want to, uh, basically
want to damage this enemy one.
So let's get out of play mode and let's double click on this message in order to move over to that line
of code.
So instead of printing out message, we're going to call the Takedamage function from that enemy one
script.
So if I move over to enemy one script, as you can see we created the function.
So we're going to call this function.
So let's call it to call it what we're gonna do.
We're going to use this call info parameter in order to access anything that we just are looking for.
So we're going to access Call info dot GameObject and call info.
Dot GameObject basically means the GameObject the circle collided with and we want to access the enemy
one script.
So let's get that.
And to get that we're going to use this dot get component.
And the component we're looking for is enemy one or enemy one script.
You can simply say enemy one.
Then make this first bracket.
And in there we created that Takedamage function.
So let's access that or let's simply call it here.
So we're going to type this dot and we're going to call the Takedamage function.
And then make this first bracket as a argument.
It's going to take the damage amount as we pass the argument, as you saw earlier.
So if you just put a three, then our enemy one just gonna die right away since we already given three
unit of health.
But I want to subtract by one unit.
So that's why as a damage amount I'm going to pass one.
And let's close that up with semicolon.
So that enemy one game object just gonna lose one unit of health as soon as we're gonna attack on it.
And now we're gonna press down Ctrl s to save our script and then move over to unity.
So as soon as we're gonna damage this enemy one, we will able to notice this.
Maxhealth just gonna move over to.
Sorry.
Uh, we have given three.
It shouldn't be.
It should be five.
Uh, I don't know.
We have given, uh uh, three, but I don't know.
I really apologize that we have given five Instead of giving five, I'm gonna pass three as a maxhealth.
Uh, so I'm gonna pass three right over here.
Now, this is fine.
So this seems pretty much cool by now.
So if we attack on this enemy one game object three times, then it's gonna lose all of his health.
And once it's lose all of his health, then what we're doing, if we move over to enemy one script we
call calling the die function.
As you can see, once our maxhealth is less than or equal to zero, then we calling the die function
and die function.
Simply printing out this message in the console says enemy one died.
As you can see.
So now let's move over to unity and let's select the console tab.
And let's click on this play button in the top in order to play our game.
And let's attack on this enemy one game object.
So, uh, once we are gonna attack three times, it's gonna lose all of his health, then die function.
Just gonna call out and die function.
Just going to print out the message says enemy one died.
So let's attack on it.
So if I attack so maxhealth we can see it's now became two.
If we attack second time, we can see it's now one.
And if we attack third time, then it's going to be zero.
And we can see max health is zero.
And as soon as it reaches zero, if we move over to console tab, enemy one died.
Message just getting pop up every single time.
Why?
Because we're calling inside this update function.
And we do know that update function runs every single frame.
So every single frame it.
Just checking the max if is then just calling that I function.
So that's why every single frame the die function is calling out, and the die function simply printing
out this passage over again and again.
So we need to show up a die, something like that.
We need to show that up.
So death animation we need to play up this enemy one game object.
So I'm going to get out of play mode.
So I hope you get the idea that how you can do it.