We're gonna do the same thing with this heart.
So before we collect the heart, we just want to check off this player back cell.
So if I select my player in the hierarchy, then go over to inspector and then player skip.
We can see the max health of this player.
By default we have given a five.
So if the max of this player game object is already five or greater than five, if that is the case,
then we don't want to collect this heart.
And let's suppose the max health of this player is lesser than five, something like four.
Then we're gonna collect this heart.
So before we collect our heart, we just want to check.
Check.
So what we need we need a reference to our player skip.
Then we can access the max health.
And we're gonna do it inside this player.
Collectible script.
Inside this player collectible.
Skip, we're gonna collect the heart.
And before we collect the heart, we just want to check the max health.
So that's why we need a reference of this player, skip.
And we can access the max cell.
And then we're going to check it To get that, we're going to open up our player collectible script
by double clicking on it in the inspector as and on that script.
And let's go to top of our class and let's grab the reference.
So are we looking for our player Skip.
So we're going to type private.
It doesn't need to be public.
We're going to grab the reference in the start function a function and the component or the script we're
looking for is player.
And we need to name the variable.
So we're going to call it something like player.
And let's go to start function.
So this is start function get call in this start of the game.
So in the start of the game we're just going to grab the reference.
And we can notice that the player script is sitting on this player game object itself.
So that's why we're going to type this start game object.
This dot game object basically means it's just referencing itself.
And now we're going to use this dot get component.
And we need to make this arrows.
So the component we're looking for is our player skip.
So inside these arrows we need to pass our player script name, which is player, and then make this
first bracket and then close that off with semicolon.
Now we have the player script.
And now what we're going to do.
We're going to put this player script into this player script variable.
As you can notice we created up over here.
So I'm going to put it into our player.
So we're going to say player equal to this.
So what we basically doing we just getting we just taking this player script.
As you can see in the Inspector we are taking this player script and we're storing into this player
skip variable.
As you can see right over here in the start of the game we're just setting player equal to this dot
game object.
And the component we're looking for is player.
Or you can simply assume we're just taking that player skip.
And we're storing into this player skip variable.
And now what we're going to do we're gonna uh basically uh check the Maxell.
But before we check, let's, uh, let's do something basically, uh, if we just go over to unity editor,
then a hierarchy, then collectibles, and then choose this game object.
Then go over to Inspector.
As you can notice, we just tag as a heart tag.
As you can notice in Inspector.
So we want to check the check the game object.
If we trigger it with a the game object has a tag called heart.
Then we need to collect that.
And before we collect we need to check the max health of it.
So we will go to our script and we're going to use this Ontriggerenter 2D function.
Why?
Because if we move over to unity a unity and then select this heart game object, then go over to Inspector
Polygon Collider 2D, we can see that we just check this is trigger.
So that's why we're going to use Ontriggerenter 2D function in order to check the trigger.
So we will go to our script.
And over here we're going to do and this function on trigger enter 2D function get called when we trigger
with something.
And we can extract information about what we trigger it with by taking this collision parameter.
So what we're going to do over here we're going to check.
So let's make if a condition and then make this first bracket and some curly brackets.
And what we want to check over here.
We basically want to check if the game object we trigger it with has a tag called hard.
That means we need to collect that hard.
So we're going to use this collision parameter.
So we're going to type collision dot game object and collision dot game object basically means the game
object we trigger it with.
So in our case we're going to trigger with that heart.
So collision dot game object over here.
It's going to be that hard game object.
And now what we're going to do we're going to access the tag of it.
So we're going to type this dot tag.
And we will check if the tag is equals equals to heart.
If that is the case that means we need to collect the heart.
So what we will do over here.
Uh so we need to check before we collect the heart.
We just need to check if the max health of our player game object is already something like five.
So if I select my player game object in the hierarchy, then go over to my player skip in Inspector,
we can notice the max cell is already five.
So if that is the case, that means we don't want to collect.
But if the max cell is something like less than five, uh, some less than five, something like four,
then we just want to collect.
So what we will do, we just want to access the player max cell.
So we will check.
So we're going to use another if condition inside this if and then make this first bracket and some
curly brackets.
And what we want to check over here.
We basically want to check if the max health of this player is lesser than or equal to five.
Something like that, or less than five or equal to five.
If that is the case, then we want to collect our heart, else we're not gonna collect at all.
So we're going to access the max cell.
So what we will do, we do know that we have the player skip, as you can see.
And inside that player skip, we have the max health parameter.
So we're going to access that.
So what we will do we will go to our player.
Skip.
Then we can access Dot Maxell.
But as you can notice, we're not able to access that.
Why?
Because we just marked as serialized field.
We need to mark as public so we can, uh, we can just access that parameter from another class as well.
So what we basically have to do, we need to move over to our player Skip.
And over there we have the maxhealth, as you can notice.
And we just mark as serialized field so we can see underneath our inspector.
So instead of making serialized private we basically need to make this whole thing public or this variable.
So we basically have to type this public int Maxhealth.
And now we can access this parameter from any other from any class that we want.
So we're going to press down Ctrl S to save our player skip.
And now we're gonna move over to Player Collectible Script.
And we are ready to access the Maxhealth.
So what we can simply do by now, we can access player dot dot As you can notice that we can access
the max.
As you can see, if it's less than or equal to five.
If that is the case, then we want to collect our hell.
So I don't know.
We need to pass our five, or we basically can check if it's lesser than five.
If that is the case, then we just want to collect.
So what are we doing?
Firstly, we're checking if we collided with the hard game object or not.
And we're checking if the tag is hard.
And definitely we're gonna trigger with this hard game object.
So if I move over to unity editor and select this hard game object and then go over to Inspector.
As you can notice, we just tag it as a hard tag.
So we just definitely gonna trigger with this hard game object.
As you can notice if that is the case.
And also we're checking if the max health of our player is lesser than five.
If that is the case, that means we need to collect that hard, else we're not gonna collect at all.
So for now, we're gonna print out a message, something like we need to collect our heart.
So we're going to type this debug dot log in order to print out the message.
And we're going to say something like collect heart.
If our max heart collect heart.
And let's close that up with semicolon.
Else if our max health is not lesser than five, lesser than lesser than five basically means our max
health is already something like five or greater than five.
Then we need to stop collecting our heart.
So that's why else if our max health is not lesser than five, means we already have the max health
of our player game object is five, then we don't want to collect.
So that's why we're going to print out a message something like don't collect heart.
So we're going to type this debug.log in order to print out the message.
And we're going to say something like don't, don't collect, collect the heart, something like that.
And we're going to close that off with semicolon.
And we're going to press down Ctrl S to save our skip.
And now we're going to move over to unity.
And you will notice as soon as we're going to trigger with this heart cam object, the mesh is just
going to pop up says don't collect a heart.
Why?
Because the max health of this player object is already five.
So that's why we don't want to collect.
So that's why we just printing out that message with saying don't collect the heart.
So if I select my player game object in a hierarchy, then go over to Inspector player skip.
We can notice the max health is five.
So let's click on this play button in the top in order to play our game.
And let's try to collect this heart.
And you can.
Are you just gonna see that message in our console tab.
Just going to print out something like don't collect heart since we already have the Max health five.
So if I trigger with this heart game object, as you can see, as soon as we trigger, we can see in
our console tab it says don't collect the heart because the max health of this player game object is
already five.
And if we make this max ultra something like four and then trigger with this hard game object, we're
gonna able to notice collect hard up message that's going to pop up since we're checking with our maxhealth
a lesser than five.
So if I trigger by now, we can notice that as soon as we triggering the message is getting pop up in
our console tab, click hard.
Since the maxhealth is not a five.
As you can notice in the inspector underneath our player script.
So the max health is four.
So that's why we want to collect our heart.
And let's suppose the max health is already five.
Then we don't want to collect this heart.
So basically that is the thing.
Uh, so we will get out of play mode.
And now what we need to do instead of printing out message, we basically need to collect this heart.
To do that, we're gonna double click on this message in order to move over to that line of code.
And instead of printing out message, we just want to collect the heart.
And in order to collect the heart, we basically need to increase our max health by one unit.
So we will go to player skip, and then we're going to access the max health.
And top of that we're just going to add one more unit something like plus equal one.
Or you can simply say we're just adding one or you can simply type this plus plus it basically means
you just adding one.
So if the player max health is less than five, let's suppose the player max health is four.
Then what we're doing we just adding as you can notice, player dot max and basically accessing the
max health of our player.
And top of that we just adding one unit.
So four plus one is going to be five.
So the maximum now it's going to be five.
And once it's five then what we need to do we need to play the collected animation clip.
Also we need to disable the Polygon Collider 2d, 2D of this heart game object.
So if I select my heart in the hierarchy underneath my collectibles game object and then go over to
Inspector.
We can notice we assign it heart and we added a collider to the Polygon Collider 2D called Polygon Collider
2D.
So we need to disable as soon as beginner trigger with this heart game object.
So we're not going to able to trigger with two eyes.
So that's why we need to basically we need to disable this polygon collider 2D.
And we also need to destroy are basically we also need to play the heart collected animation clip.
And we do know that that animation clip is sitting on our child heart sprite game object.
So if I select our heart sprite in our underneath our heart game object, then go over to Inspector.
We can notice inside this heart sprite we have this animator component which is sitting.
And if we move over to animator tab, we basically need to trigger this collected parameter.
And basically we doing the same thing as we have done with this fruit game object which is apple.
So we need a reference of this tile game object, that animator component, and we can trigger this
collected parameter and we don't know what we're going to do that it's going to play the collected animation
clip.
So what we need now we basically need to access this sprite uh basically the child game object.
So firstly we will move over to our script and we need to disable the Polygon Collider 2D.
So what we will do we will type this collision game object after collecting the heart.
So collision dot game object basically means the game object we trigger it with.
So we definitely gonna trigger with this heart game object.
So over here collision dot game object basically means that heart game object.
And now what we want to access we want to access the Polygon Collider 2D.
So we're going to type this dot get component.
And the component we're looking for is Polygon Collider 2D.
And make sure you you pass the same component name.
So if I move over to unity editor and then select this heart game object and move over to Inspector
As you can notice, we attach the components of Collider 2D.
It's Polygon Collider 2D.
So we need to make sure we just pass the same thing.
So we're going to move over to script.
And inside these arrows we're going to pass our Polygon Collider 2D.
Basically we just want to access that component.
And we need to make these brackets this first bracket.
And after accessing it we want to access.
Basically we have to type this dot.
And we're going to set enable basically accessing that.
And we're going to set it equal to false.
False basically means we're disabling this polygon collider 2D.
And let's suppose you just pass in dot enable equal to true.
It's basically means that you just enabling your polygon collider 2D since we want to disable.
So that's why firstly we just going over to our hard game object as you can notice.
Then we just going over to our Polygon Collider 2D basically this Polygon Collider 2D and we just are
accessing basically we just using this dot enable in or in order to access that polygon collider 2D
uh, basically this, uh, this and we just setting equal to false basically means we just unchecking
this polygon collider 2D.
Or we can simply say we just disabling that.
And after disabling that what we need to do we basically need to play the animation clip.
And we do know that that uh, collected animation clip, basically the animator component is sitting
on this hard sprite, basically child of this hard game object.
And this will be the first child.
As you can see, hard sprite and the first child index is just gonna be zero.
So what we need to do, we basically want to access the first child, basically that sprite game object.
Uh, to get that we're going to use this collision dot game object and collision dot game object over
here basically means the game object, basically how the hard game object.
And now we're going to use this transform and dot.
We're going to use this get child function.
And the child we're looking for is our heart sprite.
And that heart sprite is our first child.
And first child index is going to be zero.
So inside this bracket we basically have to pass the index which is gonna be zero.
And now we're going to access the game object.
And now we're going to get the component.
So dot get component.
And the component we're looking for is animator which is sitting on our hard sprite game object.
So we're going to type this animator animator and then make this first bracket.
And now we are ready to trigger that collected parameter.
And we do know that if we just trigger that collected parameter it's going to play the animation clip
which is the heart collected.
So we're going to use this dot set trigger function.
And in make this brackets and inside quotation you need to pass the same parameter name.
And if we move over to unity and select this other sprite and then move over to animator tab, we can
see the parameter name is collected.
So make sure inside quotation you pass the parameter name and make sure you spelling it correct.
Else it's not gonna work at all.
So we're going to pass our collected and then close that up with semicolon.
So what we're doing basically firstly we're just going over to our collision dot GameObject basically
means this hard game object as you can see.
Then we're just going over to transform.
So firstly we're just going over to this hard game object.
And then we just are going over to this transform.
And by this transform component we basically accessing accessing the first tile.
Or you can simply say this sprite game object.
And then after going over to hard game object, as you can see we're just getting the animator component.
Basically we're just going over to this sprite, uh, than uh, than this sprite game object, then
going over to this animator component of this sprite, and then we're just triggering this collected
parameter, as you can notice right over here.
So we're just going over to that sprite game object and then accessing the animator as you can notice.
And then we're using dot set trigger function in order to trigger that.
So uh and which is collected as you can see now it's going to play the animation clip.
And we do know that it's the animation just completing after one second.
Basically within one second it just completing the whole animation clip.
So that's why after one second we want to destroy this hard game object.
So we, we want to destroy this hard game object.
After playing the animation clip of this hard game object completely.
And we do know that after one second we just completing, uh, totally.
So that's why are we going to use this destroy function in order to destroy and this as a first argument,
it's going to take the game object that we want to destroy.
So we want to destroy that hard game object.
So we're going to pass our collision dot object basically means the heart game object simply comma.
Second thing, it's going to take the time.
Or if you just don't put the second parameter and you just go ahead and then close that off with semicolon,
then the heart game object just gonna destroy right away.
So we don't want that.
So that's why we just type comma as a second parameter.
We can just pass the time.
And we do know that after one second it just completing the whole animation clip.
So that's why we're going to put one f as a second argument.
And then we're going to close that off with semicolon.
And let's press down Ctrl s on our keyboard in order to save our script.
And now let's move over to unity.
And you will notice as soon as we're going to trigger with this heart game object, it should be collected,
but it's not going to do it all.
Why?
Because we checking the max health.
If it's greater than if it's lesser than then we just collecting else we not doing so.
The message should be pop up in our console tab.
Once we're gonna trigger with this heart game object says stone collect heart.
Why?
Because the Maxwell is already five.
So if I select my console tab and then click on this play button, and you're gonna notice as soon as
we're gonna trigger with this heart game object, or you can simply say collide with this heart game
object, the message just gonna pop up says don't collect heart because we already we already have the
max health of this player game object is five.
So I'm going to select my player game object in the hierarchy.
Then go over to Inspector and then player Skip.
As you can notice, the max health is already zero or sorry already five.
And if we trigger with this heart, as you can see, as soon we trigger it in our console tab, message
just gets pop up.
Don't collect heart because the Maxhealth is already five.
And let's suppose if we make that four and we do know that four is now less than five.
So it basically means we're gonna collect this heart by now.
And the max health which is increasing by one unit.
So if I just collect this heart, as you can see, as soon as we collect it, the Maxwell now becomes
five and the five and the hard game object we can notice it's not anymore in the hierarchy as well as
in the scene view after playing the collected animation clip completely, because we just are destroying
that after one second.
So I hope you get the idea and let me explain one more time.
So I'm going to get out of play mode.
So if I move over to my screen, firstly, firstly we are using this Ontriggerenter 2D function in order
to check with the trigger, and we are checking if the game object that we trigger it.
If that game object has a tag called heart, then what we're doing.
Firstly, we're checking the max health.
If the max cell is less than five of our player, basically means the max health is already five or
less than five, then we collect in and if the max health is already five, then we just printing out
the message in the console tab.
Don't collect the heart as you can notice.
So I hope you get the idea that how you can do it.