Now what we will do, we will collect this apple and we need to play that collected animation as we
created earlier.
So what we will do for that, I'm going to create a separate script for uh, for Collecting Apples heart
and all of those things.
To do that, we're going to move over to our project app assets and then script folder.
Let's right click there, create uh create then go over to scripting.
Then click on this Monobehaviour script.
And you can name that script something like collectables or player collectibles.
I already created a script called Player Collectibles, as you can notice right over here.
So I'm going to select my player GameObject in the hierarchy.
And let's select that script and drag it over to this Add Component section.
Make sure you attach that.
Or you can simply click on this add component.
And you can find this script by typing name of the script.
And then click on it in order to attach that.
And now we're going to open up our player collectibles by double clicking on it, and we will go to
top of our class.
And we're going to declare a variable to just keep track of how many coins we sorry, how many apples
we collected so far.
So it doesn't need to be public, but for explanation, I'm just gonna make it public, and later on
we're gonna make that feel private to just keep our code little bit a little bit clean.
So we're going to make a serialized file.
Uh, but before we do, let's give it a header or let's give it a title.
So we're going to call it collect tables.
Sorry.
Inside quotation we're gonna say collect tables collect tables something like that.
And we're going to make serialized will.
And also I apologize if the spelling is wrong of collectibles.
I really apologize for that.
And this is going to be private and this will be integer like whole numbers zero, one, two, three,
four.
And we can call something like current.
Apple's current app Apple's app.
Sorry Apple's.
And let's close that up with semicolon.
And we're going to move over to this start function.
So this start function get call in the start of the game.
So in the start of the game we're going to set our current apples to zero.
Why.
Because in the start of the game right away we're not going to collect this apple.
So that's why in the start of the game I'm gonna set current apples equal to zero.
And then close that up with semicolon.
And we're going to remove this update function we don't need.
And we're going to use Ontriggerenter 2D function y.
So if I move over to unity editor and then select this Apple game object, then go over to Inspector,
then Circle Collider 2D.
As you can see, we just check Ace Trigger to true.
So that's why we're going to use Ontriggerenter 2D function instead of Oncollisionenter.
2D function.
So we're going to move over to our script, and we're going to use this on uh, on trigger a trigger
into 2D function.
So this function get called when we trigger with something we do know.
And we get, we can extract information with this collision parameter what we trigger it with.
So what we're going to do we're going to check the tag if it's Apple.
And if we move over to unity and select this Apple GameObject in the hierarchy, then go over to Inspector.
As you can notice, we just assigned this Apple GameObject as a Apple tag.
So now what we're going to do.
We're going to check the tag of the GameObject that we triggered it with.
If that GameObject tag has a tag called Apple, then we need to collect that.
So we're going to use this if condition.
Let's make first bracket and some curly brackets.
And what we're going to do over here we're going to check.
So we will use this collision parameter.
So we're going to type collision.com object and collision.com object over here.
Basically means the game object player trigger it with and definitely player triggered are definitely
player just gonna trigger with that Apple game object.
So over here collision game object basically referencing that Apple game object.
And now what we're gonna do we're gonna access the tag of it.
So we're going to type this tag and we will check if the tag is equals equals to our apple.
If that is the case then we need to collect our apple.
But before we collect our apple we just want to check if we trigger it with that apple or not.
So we're going to print out a message.
So so for that we're going to type debug dot log.
And we will say collect Apple something like that collect app.
And let's close that up with semicolon.
And we're going to press down Ctrl s on our keyboard.
But before I do I'm just gonna arrange the and arrange this.
You can say code to just keep it clean.
I want just like this.
So that's why.
And we're going to press down Ctrl s to save our script.
And now let's move over to unity.
So as soon as we're going to trigger with this Apple game object, that message is going to pop up.
Why?
Because we assigned this apple as an Apple tag and we're checking there if we trigger it.
The game object has a tag called Apple.
If it is, then we just simply printing out that message as using which is a collect apple.
So if I select my console tab and then click on this play button in the top in order to start our game,
or in order to play our game.
And you're gonna notice that as soon as we're going to trigger with this apple.
So just wait a moment and you will see that.
So let's trigger with this apple.
As you can see, as soon as we trigger it we can see the message in our console tab.
Just getting pop up says Collect Apple.
So instead of printing out message, we just want to increase our apple.
And also we need to trigger this trigger this if I select my Apple GameObject in the hierarchy.
Uh, firstly let's get out of play mode.
So if I select my apple and then this Apple icon.
So the animator component is setting on this Apple icon game object, not the parent one.
Basically it's just sitting on this Apple icon child game object.
And this is our child and this will be our first child.
And if you just duplicate this apple icon, then it's going to be the second child.
And the index it off.
It's just going to be one.
So since this Apple icon index, uh, is a first child.
So index uh, basically you can simply say the index of it, it's going to be zero.
And if we duplicate this apple icon one more time than the duplicate one index.
Gonna be sorry.
The duplicate one index will be a one.
So since we're looking for our this Apple icon.
So as a child we need to pass zero because zero basically referencing the first child.
And we can notice that if we select our Apple icon then go over to Inspector tab.
We can see the animator component is setting on this Apple icon.
So we need this animator component before we trigger this collected parameter.
So we basically need to go over to this Apple icon GameObject.
Basically the child of this Apple game object.
And then we can just go over to animator.
And we're gonna get that.
And then we're going to trigger it.
So let's open up our script.
And instead of printing out message we just want to increase our current apples.
So we're gonna set our current apples equals equals plus.
Plus like that basically means adding one.
So if we if we collect our first apple.
So as you can notice in the start of the game, current apple is zero.
So as soon as we get a trigger with that Apple we adding one.
As you can notice current Apple Plus plus basically adding one.
So it's going to be something like zero plus one zero plus one.
We do know it's going to be one means we just increasing our current apples.
And after increasing that what we need to do we need to trigger this collected parameter and this.
And we can notice that animator component is sitting on this tile game object of this Apple game object.
So we need to get the tile.
And to get the child we basically have to use a transform.
And this is since this is the only child or this is the first tile you can say.
So the index of it's going to be zero.
So let's go to our script and we're going to use this collision dot game object and collision dot game
object over here basically means the Apple game object.
So we need to get to the tile game object.
And in order to get that we're going to use dot transform.
And then dot we're going to use this get tile function.
And in these brackets we need to pass the index.
So what we simply doing we just going over to this Apple game object first then going over to transform.
As you can notice in Inspector.
And by this transform we're just getting the tile as you can see.
So we're looking for this Apple icon.
And the Apple icon index is going to be zero since this is our first tile.
So that's why.
So inside this get tile function we're going to pass zero basically means that Apple icon game object
which is going over to that.
And now what we're going to do we're going to type this game object.
And we need to get the animator component.
So we're going to use this dot get component.
And the component we're looking for is animator.
So make this arrows.
And inside this arrows just pass the component you are looking for.
So we're looking for animator.
So we're going to type this animator.
Sorry animator animator.
And then make this first bracket and then what we're going to do.
So now we have a reference of that animator component.
So we need to trigger that collected parameter.
And in order to trigger that we need to use dot set trigger function.
And this set trigger function in brackets.
It's going to take the parameter name inside quotation.
And make sure you just go to unity editor and check the parameter name.
And we can notice that we have the parameter name.
Sorry, we have the parameter name collected as you can notice inside our animator tab.
So now let's move over to our script.
And inside this quotation we're going to pass our uh parameter name.
So the parameter name is collected.
And make sure you spelling it Correct.
Else it's not gonna work and press down Ctrl S to save your script.
And then.
And then let's move over to unity.
So let's go over to unity and we will see.
So what we simply doing.
Firstly we going over to this Apple game object then going over to transform.
And by this transform we just going over to this animator component basically firstly we just moving
over to this Apple game object.
Sorry Apple game object transform as you can see.
And by this transform we're just going over to this Apple icon game object.
And then this animator component.
As you can notice in Inspector.
And by getting this animator component we just triggering this collected parameter.
As you can notice in our animator tab.
And now let's see what's gonna happen.
And I'm gonna select my player GameObject in the hierarchy to able to see my player collectibles script.
As you can notice in the inspector, uh, inside this player game object.
So current apples by default is zero.
So let's click on this play button in the top in order to play our game.
So as soon as we're going to collect this parameter you will see current Apple just gonna increase by
one.
And after that we just playing that animation clip which is the collected as you seen earlier that we
created.
Uh, so we're gonna collect this apple.
So as you can see, as soon as we just triggered it with that current apples, we can notice current
apples is one and it's became two.
Why?
Because we need to disable the current apples Collider 2D, uh, as soon as we're gonna collect that.
So that's why if I just, uh, trigger one more time, as you can see, our current apples just keep
increasing.
Because if we move over to hierarchy, we can see Apple game object is still in the scene view.
And you can notice right over here if I select the move tool in the scene view.
As you can notice, the Circle Collider is still still over here, as well as the sprite or this Apple
GameObject is still in the game view.
So after some period of time, we need to destroy that.
And we do know that collected animation clip just playing for one second.
So after one second we just want to you can simply say want to destroy.
But before we destroy, we need to disable this Collider 2D as we attach inside this Apple GameObject.
And we do know that as soon as we're gonna collide with that, we just keep increasing our current apples.
So if I just trigger one more time, as you can notice underneath our player collectible script in Inspector
Current, apples became one four.
And if we trigger one more time, it became five.
So we need to disable that circle Collider 2D of this Apple game object as soon as we're gonna trigger
with that.
And after that, once again, we're just gonna destroy our apple from the scene view as well as in the
hierarchy as well as in the game view.
Sorry, scene view and game view.
So let's get out of play mode.
And for that, what we're gonna do, we're gonna move over to our script over here.
As you can notice, we're collecting.
After collecting that, we're gonna disable our, uh, basically the Circle Collider 2D of that Apple,
uh, game object.
So we're going to use this collision dot game object.
And we do know that collision dot game object basically means the game object we trigger it with.
So in our case it's going to be the apple.
And now what we're gonna do we're gonna get this component or circle Collider 2D.
So we're going to use this dot get component and make these arrows.
And inside these arrows you need to pass the component name that we're looking for.
So if I select my Apple GameObject in the hierarchy then go over to Inspector then Circle Collider 2D.
As you can see, we need this circle Collider 2D and we need to set the enable basically uncheck.
Basically we need to disable that as soon as we're going to trigger with that.
So that's why we're inside these arrows.
We're going to pass this circle Collider 2D.
Because this circle Collider 2D is attached to that Apple game object.
And now we're going to make this brackets.
And to get that and now let's use dot enabled.
So make sure you just use this dot enable.
And if you set equal to false it's basically means you just disabling that circle Collider 2D.
And if you pass true instead of false that A that's basically means you enabling that circle collider
2D.
So since we want to disable.
So that's why we just type dot enable equal to false, we basically disabling that.
And after disabling that we also need to destroy that Apple game object.
And we do know that it's playing after.
So the collected animation clip just playing uh or playing with one second.
So after one second, we also need to destroy the Apple game object itself.
And to destroy, we're gonna use this destroy function and this destroy function as a first argument.
It's going to take the game object that we want to destroy, since we want to destroy our Apple game
object.
So we're going to pass this collision dot game object means the Apple game object.
And second thing comma.
We can specify the time.
And if you don't specify the time and you just close that off with semicolon, that means Apple just
gonna destroy it right away, which we don't want since we want to play the collected animation clip
of that Apple.
So that's why as a second argument, we're going to pass one F basically means after one second we're
destroying our Apple game object.
And now we're going to close that up with semicolon.
And let's press down Ctrl s to save our escape.
And now let's move over to unity.
So as soon as we're gonna collect this file.
Firstly, we're disabling the Circle collider 2D of this apple.
And after that we are destroying this Apple GameObject.
Uh, one second after one second.
So just wait a moment.
Uh, so just wait a moment.
And if I select my player game object in the hierarchy, we can notice player collectibles are underneath
our player.
Collect collectibles.
Script current apples is zero.
So let's click on this play button in the top in order to play our game.
And let's collect this, uh, apple uh, or let's trigger with this apple as soon as we can, as soon
as possible.
We're gonna collect that.
So now if we collect this apple, as you can see, current apples became one.
And we cannot able to notice our apple in this in interview, as well as in the game view as well as
in the hierarchy.
As you can notice, underneath our hierarchy tab, we are not able to notice our Apple game object since
we totally destroyed that.
So I hope you get the idea that how you can do it, and I'm gonna get out of play mode and you can do
the same thing with our basically with player health, we're gonna do this same thing with our player
health and make sure you, uh, basically, uh, press down control s in order to save your project.
But before I save my project, I'm gonna create an empty game object, and I'm gonna name that game
object to something like collectables.
And I will track all of the collectables into that game object in order to make our hierarchy clean
or to, uh, to keep our project clean.
So let's do that.
To do that, we're gonna move over to hierarchy, right click or hit this plus icon, then create an
empty game object and we're going to name this game object something like collectables.
So let's call it collectables.
Let's go over to Inspector Transform.
Let's right click and then reset.
And I'm going to choose an icon for that like this.
And you can notice right over here and I'm going to select my collectables GameObject in the hierarchy
hierarchy.
And let's drag it in the top so we can see all the time.
And now I'm going to take my Apple GameObject in the hierarchy.
And let's drag it off of this collectable game object.
Now it's done.
And we can just click over here to just collapse.
And as you can notice by now, our hierarchy seems a lot more clean.
So once we're gonna have the, uh, the heart animation, a heart game object, uh, because that,
uh, that's just going to be a collectible so we can just drag it on top of this collectables game object
to just keep our project clean and the hierarchy, basically.