WEBVTT

00:00.040 --> 00:06.440
Now what we will do, we will collect this apple and we need to play that collected animation as we

00:06.480 --> 00:07.680
created earlier.

00:07.880 --> 00:15.120
So what we will do for that, I'm going to create a separate script for uh, for Collecting Apples heart

00:15.120 --> 00:16.640
and all of those things.

00:16.840 --> 00:21.800
To do that, we're going to move over to our project app assets and then script folder.

00:21.840 --> 00:26.280
Let's right click there, create uh create then go over to scripting.

00:26.320 --> 00:29.040
Then click on this Monobehaviour script.

00:29.080 --> 00:33.640
And you can name that script something like collectables or player collectibles.

00:33.760 --> 00:39.720
I already created a script called Player Collectibles, as you can notice right over here.

00:39.720 --> 00:42.600
So I'm going to select my player GameObject in the hierarchy.

00:42.600 --> 00:47.080
And let's select that script and drag it over to this Add Component section.

00:47.120 --> 00:48.640
Make sure you attach that.

00:48.680 --> 00:51.320
Or you can simply click on this add component.

00:51.320 --> 00:55.000
And you can find this script by typing name of the script.

00:55.000 --> 00:57.880
And then click on it in order to attach that.

00:58.000 --> 01:03.500
And now we're going to open up our player collectibles by double clicking on it, and we will go to

01:03.540 --> 01:04.900
top of our class.

01:04.900 --> 01:12.100
And we're going to declare a variable to just keep track of how many coins we sorry, how many apples

01:12.100 --> 01:13.620
we collected so far.

01:13.980 --> 01:20.380
So it doesn't need to be public, but for explanation, I'm just gonna make it public, and later on

01:20.380 --> 01:27.340
we're gonna make that feel private to just keep our code little bit a little bit clean.

01:27.340 --> 01:29.260
So we're going to make a serialized file.

01:29.540 --> 01:33.900
Uh, but before we do, let's give it a header or let's give it a title.

01:34.180 --> 01:36.420
So we're going to call it collect tables.

01:37.140 --> 01:37.660
Sorry.

01:37.900 --> 01:45.420
Inside quotation we're gonna say collect tables collect tables something like that.

01:46.020 --> 01:48.620
And we're going to make serialized will.

01:48.620 --> 01:53.500
And also I apologize if the spelling is wrong of collectibles.

01:53.900 --> 01:55.580
I really apologize for that.

01:55.780 --> 02:02.600
And this is going to be private and this will be integer like whole numbers zero, one, two, three,

02:02.640 --> 02:02.960
four.

02:03.160 --> 02:05.440
And we can call something like current.

02:05.480 --> 02:08.920
Apple's current app Apple's app.

02:09.480 --> 02:11.000
Sorry Apple's.

02:11.120 --> 02:13.320
And let's close that up with semicolon.

02:13.440 --> 02:16.000
And we're going to move over to this start function.

02:16.000 --> 02:19.240
So this start function get call in the start of the game.

02:19.240 --> 02:23.320
So in the start of the game we're going to set our current apples to zero.

02:23.360 --> 02:23.760
Why.

02:23.920 --> 02:28.240
Because in the start of the game right away we're not going to collect this apple.

02:28.240 --> 02:33.480
So that's why in the start of the game I'm gonna set current apples equal to zero.

02:33.680 --> 02:35.760
And then close that up with semicolon.

02:36.040 --> 02:39.240
And we're going to remove this update function we don't need.

02:39.280 --> 02:42.600
And we're going to use Ontriggerenter 2D function y.

02:42.880 --> 02:48.960
So if I move over to unity editor and then select this Apple game object, then go over to Inspector,

02:49.000 --> 02:50.720
then Circle Collider 2D.

02:50.760 --> 02:54.280
As you can see, we just check Ace Trigger to true.

02:54.280 --> 02:59.860
So that's why we're going to use Ontriggerenter 2D function instead of Oncollisionenter.

02:59.900 --> 03:00.940
2D function.

03:01.580 --> 03:09.260
So we're going to move over to our script, and we're going to use this on uh, on trigger a trigger

03:09.300 --> 03:10.780
into 2D function.

03:10.780 --> 03:14.540
So this function get called when we trigger with something we do know.

03:14.540 --> 03:20.140
And we get, we can extract information with this collision parameter what we trigger it with.

03:20.140 --> 03:23.700
So what we're going to do we're going to check the tag if it's Apple.

03:23.700 --> 03:29.820
And if we move over to unity and select this Apple GameObject in the hierarchy, then go over to Inspector.

03:29.860 --> 03:34.860
As you can notice, we just assigned this Apple GameObject as a Apple tag.

03:35.140 --> 03:36.420
So now what we're going to do.

03:36.460 --> 03:40.060
We're going to check the tag of the GameObject that we triggered it with.

03:40.100 --> 03:44.500
If that GameObject tag has a tag called Apple, then we need to collect that.

03:44.500 --> 03:46.500
So we're going to use this if condition.

03:46.500 --> 03:49.340
Let's make first bracket and some curly brackets.

03:49.340 --> 03:51.980
And what we're going to do over here we're going to check.

03:51.980 --> 03:54.360
So we will use this collision parameter.

03:54.920 --> 04:00.160
So we're going to type collision.com object and collision.com object over here.

04:00.200 --> 04:06.800
Basically means the game object player trigger it with and definitely player triggered are definitely

04:06.840 --> 04:09.800
player just gonna trigger with that Apple game object.

04:09.800 --> 04:14.920
So over here collision game object basically referencing that Apple game object.

04:14.920 --> 04:18.280
And now what we're gonna do we're gonna access the tag of it.

04:18.280 --> 04:24.200
So we're going to type this tag and we will check if the tag is equals equals to our apple.

04:24.360 --> 04:28.200
If that is the case then we need to collect our apple.

04:28.240 --> 04:34.920
But before we collect our apple we just want to check if we trigger it with that apple or not.

04:35.080 --> 04:37.000
So we're going to print out a message.

04:37.000 --> 04:39.960
So so for that we're going to type debug dot log.

04:40.080 --> 04:44.800
And we will say collect Apple something like that collect app.

04:45.600 --> 04:47.760
And let's close that up with semicolon.

04:47.960 --> 04:50.920
And we're going to press down Ctrl s on our keyboard.

04:50.920 --> 04:56.180
But before I do I'm just gonna arrange the and arrange this.

04:56.340 --> 04:58.900
You can say code to just keep it clean.

05:00.020 --> 05:01.580
I want just like this.

05:01.580 --> 05:02.420
So that's why.

05:02.700 --> 05:05.500
And we're going to press down Ctrl s to save our script.

05:05.500 --> 05:07.580
And now let's move over to unity.

05:07.980 --> 05:12.980
So as soon as we're going to trigger with this Apple game object, that message is going to pop up.

05:13.020 --> 05:13.420
Why?

05:13.580 --> 05:18.820
Because we assigned this apple as an Apple tag and we're checking there if we trigger it.

05:19.060 --> 05:21.380
The game object has a tag called Apple.

05:21.380 --> 05:27.420
If it is, then we just simply printing out that message as using which is a collect apple.

05:27.420 --> 05:33.700
So if I select my console tab and then click on this play button in the top in order to start our game,

05:33.700 --> 05:35.660
or in order to play our game.

05:35.660 --> 05:40.460
And you're gonna notice that as soon as we're going to trigger with this apple.

05:40.460 --> 05:43.100
So just wait a moment and you will see that.

05:44.460 --> 05:46.980
So let's trigger with this apple.

05:47.020 --> 05:52.630
As you can see, as soon as we trigger it we can see the message in our console tab.

05:52.630 --> 05:55.150
Just getting pop up says Collect Apple.

05:55.150 --> 05:59.550
So instead of printing out message, we just want to increase our apple.

05:59.550 --> 06:06.230
And also we need to trigger this trigger this if I select my Apple GameObject in the hierarchy.

06:06.670 --> 06:08.670
Uh, firstly let's get out of play mode.

06:09.270 --> 06:14.270
So if I select my apple and then this Apple icon.

06:14.270 --> 06:19.950
So the animator component is setting on this Apple icon game object, not the parent one.

06:19.990 --> 06:23.950
Basically it's just sitting on this Apple icon child game object.

06:24.190 --> 06:28.910
And this is our child and this will be our first child.

06:28.910 --> 06:33.390
And if you just duplicate this apple icon, then it's going to be the second child.

06:33.390 --> 06:35.190
And the index it off.

06:35.190 --> 06:36.950
It's just going to be one.

06:36.950 --> 06:41.350
So since this Apple icon index, uh, is a first child.

06:41.430 --> 06:46.630
So index uh, basically you can simply say the index of it, it's going to be zero.

06:46.830 --> 06:52.410
And if we duplicate this apple icon one more time than the duplicate one index.

06:52.450 --> 06:53.690
Gonna be sorry.

06:53.730 --> 06:56.810
The duplicate one index will be a one.

06:56.810 --> 06:59.770
So since we're looking for our this Apple icon.

06:59.770 --> 07:06.330
So as a child we need to pass zero because zero basically referencing the first child.

07:06.570 --> 07:12.530
And we can notice that if we select our Apple icon then go over to Inspector tab.

07:12.530 --> 07:16.610
We can see the animator component is setting on this Apple icon.

07:16.610 --> 07:21.250
So we need this animator component before we trigger this collected parameter.

07:21.250 --> 07:24.690
So we basically need to go over to this Apple icon GameObject.

07:24.690 --> 07:27.370
Basically the child of this Apple game object.

07:27.370 --> 07:29.770
And then we can just go over to animator.

07:29.770 --> 07:31.130
And we're gonna get that.

07:31.130 --> 07:32.690
And then we're going to trigger it.

07:32.690 --> 07:34.690
So let's open up our script.

07:34.690 --> 07:39.890
And instead of printing out message we just want to increase our current apples.

07:39.890 --> 07:43.410
So we're gonna set our current apples equals equals plus.

07:43.410 --> 07:46.370
Plus like that basically means adding one.

07:46.390 --> 07:50.190
So if we if we collect our first apple.

07:50.190 --> 07:54.070
So as you can notice in the start of the game, current apple is zero.

07:54.190 --> 07:58.430
So as soon as we get a trigger with that Apple we adding one.

07:58.430 --> 08:02.630
As you can notice current Apple Plus plus basically adding one.

08:02.630 --> 08:06.550
So it's going to be something like zero plus one zero plus one.

08:06.550 --> 08:11.790
We do know it's going to be one means we just increasing our current apples.

08:11.790 --> 08:18.710
And after increasing that what we need to do we need to trigger this collected parameter and this.

08:18.910 --> 08:25.230
And we can notice that animator component is sitting on this tile game object of this Apple game object.

08:25.230 --> 08:26.790
So we need to get the tile.

08:26.790 --> 08:30.350
And to get the child we basically have to use a transform.

08:30.510 --> 08:35.710
And this is since this is the only child or this is the first tile you can say.

08:35.710 --> 08:38.630
So the index of it's going to be zero.

08:38.630 --> 08:44.230
So let's go to our script and we're going to use this collision dot game object and collision dot game

08:44.270 --> 08:47.690
object over here basically means the Apple game object.

08:47.690 --> 08:50.250
So we need to get to the tile game object.

08:50.250 --> 08:54.250
And in order to get that we're going to use dot transform.

08:54.450 --> 08:57.850
And then dot we're going to use this get tile function.

08:57.850 --> 09:00.890
And in these brackets we need to pass the index.

09:00.890 --> 09:07.010
So what we simply doing we just going over to this Apple game object first then going over to transform.

09:07.050 --> 09:09.130
As you can notice in Inspector.

09:09.130 --> 09:12.930
And by this transform we're just getting the tile as you can see.

09:13.170 --> 09:15.530
So we're looking for this Apple icon.

09:15.530 --> 09:19.850
And the Apple icon index is going to be zero since this is our first tile.

09:19.850 --> 09:20.890
So that's why.

09:21.090 --> 09:26.530
So inside this get tile function we're going to pass zero basically means that Apple icon game object

09:26.530 --> 09:28.370
which is going over to that.

09:28.370 --> 09:31.450
And now what we're going to do we're going to type this game object.

09:31.650 --> 09:34.250
And we need to get the animator component.

09:34.250 --> 09:37.170
So we're going to use this dot get component.

09:37.170 --> 09:39.770
And the component we're looking for is animator.

09:39.770 --> 09:41.090
So make this arrows.

09:41.090 --> 09:45.990
And inside this arrows just pass the component you are looking for.

09:46.070 --> 09:47.670
So we're looking for animator.

09:47.670 --> 09:49.510
So we're going to type this animator.

09:49.750 --> 09:53.950
Sorry animator animator.

09:53.950 --> 09:57.590
And then make this first bracket and then what we're going to do.

09:57.590 --> 10:01.110
So now we have a reference of that animator component.

10:01.110 --> 10:04.430
So we need to trigger that collected parameter.

10:04.430 --> 10:08.790
And in order to trigger that we need to use dot set trigger function.

10:08.790 --> 10:10.990
And this set trigger function in brackets.

10:10.990 --> 10:14.470
It's going to take the parameter name inside quotation.

10:14.470 --> 10:18.630
And make sure you just go to unity editor and check the parameter name.

10:18.630 --> 10:21.510
And we can notice that we have the parameter name.

10:21.550 --> 10:28.710
Sorry, we have the parameter name collected as you can notice inside our animator tab.

10:28.710 --> 10:31.150
So now let's move over to our script.

10:31.390 --> 10:36.750
And inside this quotation we're going to pass our uh parameter name.

10:36.750 --> 10:39.870
So the parameter name is collected.

10:39.870 --> 10:41.930
And make sure you spelling it Correct.

10:41.970 --> 10:46.330
Else it's not gonna work and press down Ctrl S to save your script.

10:46.330 --> 10:46.890
And then.

10:47.370 --> 10:49.450
And then let's move over to unity.

10:49.650 --> 10:52.570
So let's go over to unity and we will see.

10:52.570 --> 10:54.050
So what we simply doing.

10:54.090 --> 10:59.130
Firstly we going over to this Apple game object then going over to transform.

10:59.130 --> 11:05.650
And by this transform we just going over to this animator component basically firstly we just moving

11:05.650 --> 11:07.570
over to this Apple game object.

11:08.290 --> 11:11.770
Sorry Apple game object transform as you can see.

11:11.810 --> 11:16.370
And by this transform we're just going over to this Apple icon game object.

11:16.370 --> 11:18.330
And then this animator component.

11:18.330 --> 11:20.570
As you can notice in Inspector.

11:20.570 --> 11:25.450
And by getting this animator component we just triggering this collected parameter.

11:25.450 --> 11:28.770
As you can notice in our animator tab.

11:29.010 --> 11:31.410
And now let's see what's gonna happen.

11:31.410 --> 11:38.450
And I'm gonna select my player GameObject in the hierarchy to able to see my player collectibles script.

11:38.450 --> 11:43.430
As you can notice in the inspector, uh, inside this player game object.

11:43.430 --> 11:45.710
So current apples by default is zero.

11:45.710 --> 11:49.990
So let's click on this play button in the top in order to play our game.

11:49.990 --> 11:55.470
So as soon as we're going to collect this parameter you will see current Apple just gonna increase by

11:55.470 --> 11:55.990
one.

11:55.990 --> 12:02.150
And after that we just playing that animation clip which is the collected as you seen earlier that we

12:02.190 --> 12:02.990
created.

12:03.350 --> 12:05.790
Uh, so we're gonna collect this apple.

12:06.070 --> 12:11.910
So as you can see, as soon as we just triggered it with that current apples, we can notice current

12:11.950 --> 12:15.030
apples is one and it's became two.

12:15.190 --> 12:15.670
Why?

12:15.870 --> 12:23.230
Because we need to disable the current apples Collider 2D, uh, as soon as we're gonna collect that.

12:23.230 --> 12:30.390
So that's why if I just, uh, trigger one more time, as you can see, our current apples just keep

12:30.390 --> 12:31.070
increasing.

12:31.070 --> 12:36.950
Because if we move over to hierarchy, we can see Apple game object is still in the scene view.

12:36.990 --> 12:41.570
And you can notice right over here if I select the move tool in the scene view.

12:41.850 --> 12:49.010
As you can notice, the Circle Collider is still still over here, as well as the sprite or this Apple

12:49.010 --> 12:51.090
GameObject is still in the game view.

12:51.250 --> 12:54.610
So after some period of time, we need to destroy that.

12:54.770 --> 12:59.410
And we do know that collected animation clip just playing for one second.

12:59.450 --> 13:05.170
So after one second we just want to you can simply say want to destroy.

13:05.170 --> 13:12.010
But before we destroy, we need to disable this Collider 2D as we attach inside this Apple GameObject.

13:12.010 --> 13:18.890
And we do know that as soon as we're gonna collide with that, we just keep increasing our current apples.

13:18.970 --> 13:25.610
So if I just trigger one more time, as you can notice underneath our player collectible script in Inspector

13:25.650 --> 13:27.850
Current, apples became one four.

13:27.890 --> 13:30.850
And if we trigger one more time, it became five.

13:30.850 --> 13:38.110
So we need to disable that circle Collider 2D of this Apple game object as soon as we're gonna trigger

13:38.110 --> 13:38.750
with that.

13:38.750 --> 13:44.710
And after that, once again, we're just gonna destroy our apple from the scene view as well as in the

13:44.710 --> 13:47.190
hierarchy as well as in the game view.

13:47.390 --> 13:49.390
Sorry, scene view and game view.

13:49.390 --> 13:51.190
So let's get out of play mode.

13:51.190 --> 13:55.630
And for that, what we're gonna do, we're gonna move over to our script over here.

13:55.630 --> 13:57.670
As you can notice, we're collecting.

13:57.710 --> 14:04.670
After collecting that, we're gonna disable our, uh, basically the Circle Collider 2D of that Apple,

14:04.830 --> 14:05.950
uh, game object.

14:05.950 --> 14:08.670
So we're going to use this collision dot game object.

14:08.670 --> 14:13.790
And we do know that collision dot game object basically means the game object we trigger it with.

14:13.790 --> 14:16.310
So in our case it's going to be the apple.

14:16.470 --> 14:21.510
And now what we're gonna do we're gonna get this component or circle Collider 2D.

14:21.510 --> 14:25.150
So we're going to use this dot get component and make these arrows.

14:25.150 --> 14:29.670
And inside these arrows you need to pass the component name that we're looking for.

14:29.790 --> 14:36.010
So if I select my Apple GameObject in the hierarchy then go over to Inspector then Circle Collider 2D.

14:36.050 --> 14:41.850
As you can see, we need this circle Collider 2D and we need to set the enable basically uncheck.

14:41.850 --> 14:46.290
Basically we need to disable that as soon as we're going to trigger with that.

14:46.410 --> 14:48.810
So that's why we're inside these arrows.

14:48.810 --> 14:51.090
We're going to pass this circle Collider 2D.

14:51.130 --> 14:55.490
Because this circle Collider 2D is attached to that Apple game object.

14:55.490 --> 14:57.530
And now we're going to make this brackets.

14:57.730 --> 15:01.930
And to get that and now let's use dot enabled.

15:01.930 --> 15:05.570
So make sure you just use this dot enable.

15:05.770 --> 15:12.250
And if you set equal to false it's basically means you just disabling that circle Collider 2D.

15:12.570 --> 15:19.730
And if you pass true instead of false that A that's basically means you enabling that circle collider

15:19.770 --> 15:20.250
2D.

15:20.370 --> 15:22.370
So since we want to disable.

15:22.370 --> 15:27.970
So that's why we just type dot enable equal to false, we basically disabling that.

15:28.250 --> 15:33.780
And after disabling that we also need to destroy that Apple game object.

15:33.780 --> 15:36.500
And we do know that it's playing after.

15:36.780 --> 15:42.060
So the collected animation clip just playing uh or playing with one second.

15:42.060 --> 15:46.460
So after one second, we also need to destroy the Apple game object itself.

15:46.620 --> 15:52.260
And to destroy, we're gonna use this destroy function and this destroy function as a first argument.

15:52.260 --> 15:57.860
It's going to take the game object that we want to destroy, since we want to destroy our Apple game

15:57.900 --> 15:58.260
object.

15:58.260 --> 16:02.620
So we're going to pass this collision dot game object means the Apple game object.

16:02.740 --> 16:04.020
And second thing comma.

16:04.020 --> 16:05.940
We can specify the time.

16:05.940 --> 16:12.060
And if you don't specify the time and you just close that off with semicolon, that means Apple just

16:12.060 --> 16:19.540
gonna destroy it right away, which we don't want since we want to play the collected animation clip

16:19.540 --> 16:20.580
of that Apple.

16:20.580 --> 16:25.860
So that's why as a second argument, we're going to pass one F basically means after one second we're

16:25.860 --> 16:28.260
destroying our Apple game object.

16:28.260 --> 16:30.540
And now we're going to close that up with semicolon.

16:30.600 --> 16:33.720
And let's press down Ctrl s to save our escape.

16:33.760 --> 16:35.600
And now let's move over to unity.

16:35.720 --> 16:39.040
So as soon as we're gonna collect this file.

16:39.280 --> 16:42.840
Firstly, we're disabling the Circle collider 2D of this apple.

16:42.840 --> 16:46.600
And after that we are destroying this Apple GameObject.

16:46.680 --> 16:49.200
Uh, one second after one second.

16:49.200 --> 16:50.960
So just wait a moment.

16:52.360 --> 16:54.320
Uh, so just wait a moment.

16:54.320 --> 17:00.320
And if I select my player game object in the hierarchy, we can notice player collectibles are underneath

17:00.360 --> 17:01.000
our player.

17:01.000 --> 17:02.320
Collect collectibles.

17:02.320 --> 17:04.600
Script current apples is zero.

17:04.600 --> 17:08.720
So let's click on this play button in the top in order to play our game.

17:08.720 --> 17:15.880
And let's collect this, uh, apple uh, or let's trigger with this apple as soon as we can, as soon

17:15.880 --> 17:16.640
as possible.

17:16.640 --> 17:17.840
We're gonna collect that.

17:18.240 --> 17:25.080
So now if we collect this apple, as you can see, current apples became one.

17:25.080 --> 17:31.780
And we cannot able to notice our apple in this in interview, as well as in the game view as well as

17:31.780 --> 17:32.660
in the hierarchy.

17:32.820 --> 17:39.300
As you can notice, underneath our hierarchy tab, we are not able to notice our Apple game object since

17:39.300 --> 17:41.300
we totally destroyed that.

17:41.460 --> 17:47.300
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

17:47.300 --> 17:55.020
the same thing with our basically with player health, we're gonna do this same thing with our player

17:55.020 --> 18:03.780
health and make sure you, uh, basically, uh, press down control s in order to save your project.

18:03.820 --> 18:10.260
But before I save my project, I'm gonna create an empty game object, and I'm gonna name that game

18:10.260 --> 18:12.380
object to something like collectables.

18:12.380 --> 18:18.580
And I will track all of the collectables into that game object in order to make our hierarchy clean

18:18.580 --> 18:21.660
or to, uh, to keep our project clean.

18:21.780 --> 18:22.980
So let's do that.

18:22.980 --> 18:28.280
To do that, we're gonna move over to hierarchy, right click or hit this plus icon, then create an

18:28.320 --> 18:33.400
empty game object and we're going to name this game object something like collectables.

18:33.600 --> 18:36.320
So let's call it collectables.

18:36.680 --> 18:38.920
Let's go over to Inspector Transform.

18:38.920 --> 18:40.880
Let's right click and then reset.

18:41.080 --> 18:44.360
And I'm going to choose an icon for that like this.

18:44.680 --> 18:50.160
And you can notice right over here and I'm going to select my collectables GameObject in the hierarchy

18:50.200 --> 18:50.800
hierarchy.

18:50.800 --> 18:54.200
And let's drag it in the top so we can see all the time.

18:54.200 --> 18:57.400
And now I'm going to take my Apple GameObject in the hierarchy.

18:57.520 --> 19:01.320
And let's drag it off of this collectable game object.

19:01.360 --> 19:02.160
Now it's done.

19:02.160 --> 19:04.880
And we can just click over here to just collapse.

19:04.880 --> 19:09.840
And as you can notice by now, our hierarchy seems a lot more clean.

19:09.840 --> 19:16.200
So once we're gonna have the, uh, the heart animation, a heart game object, uh, because that,

19:16.200 --> 19:22.120
uh, that's just going to be a collectible so we can just drag it on top of this collectables game object

19:22.120 --> 19:26.280
to just keep our project clean and the hierarchy, basically.
