WEBVTT

00:00.040 --> 00:02.080
We need a reference of this text.

00:02.120 --> 00:05.040
This Apple text as well as this text.

00:05.080 --> 00:08.160
We need a reference, a reference of it.

00:08.160 --> 00:09.240
Then we can change.

00:09.400 --> 00:16.400
So if I move over to my hierarchy then canvas and then select this UI and then go over to this current

00:16.440 --> 00:18.320
Apple text current Apple text.

00:18.320 --> 00:21.520
Then go over to Inspector and then Text component.

00:21.520 --> 00:23.560
And we can change the value over here.

00:23.560 --> 00:24.800
We can set it to one.

00:24.800 --> 00:25.960
We can set it to two.

00:26.000 --> 00:27.360
We can set it to three.

00:27.480 --> 00:32.520
So basically what we need we need to go over to this current Apple text game object first.

00:32.520 --> 00:34.480
And then we need to access this text.

00:34.480 --> 00:40.160
And we're going to set this text equal to our current apples we have collected so far.

00:40.360 --> 00:46.200
So if I select my player GameObject in the hierarchy then go over to Player Collectible Script in in

00:46.240 --> 00:47.640
in in Inspector.

00:47.680 --> 00:52.840
As you can notice we have current apples zero and we showing up over here.

00:52.840 --> 00:55.960
So we also need to update this in the UI.

00:56.000 --> 00:58.600
Basically we need to set this current UI.

00:58.640 --> 01:04.280
Basically this current Apple text with our, uh, this, uh, current apples.

01:04.280 --> 01:07.400
So we need to do that to do to get the reference.

01:07.400 --> 01:09.640
So let's open up our script first.

01:09.640 --> 01:13.240
So we're going to move over to our player GameObject in the hierarchy.

01:13.440 --> 01:18.120
And let's open up the player collectible script in the Inspector by double clicking on it.

01:19.120 --> 01:21.720
And we will go to the top of our class.

01:21.720 --> 01:25.520
And we're gonna, we're gonna get that text component.

01:25.520 --> 01:31.360
And before you get the text component, make sure you just go to top of your class and include this

01:31.360 --> 01:34.480
namespace called using Unity Engine dot UI.

01:34.680 --> 01:39.360
So whenever you're working with UI, make sure you include this namespace.

01:39.360 --> 01:45.200
It's called using Unity Engine UI because that text element is a UI element.

01:45.200 --> 01:46.160
So that's why.

01:46.480 --> 01:54.440
So we're going to make a serialized field, sorry, serialized field, uh, a private and a component

01:54.440 --> 01:56.520
that we're looking for is a text type.

01:56.520 --> 02:01.440
So we're going to type this text and we can call this one something like Current Apple Text.

02:01.440 --> 02:07.480
So let's call it current Apple Text and then close that up with semicolon.

02:07.480 --> 02:12.080
And we're going to press down Ctrl s on our keyboard in order to save our script.

02:12.080 --> 02:14.000
And now let's move over to unity.

02:14.400 --> 02:19.440
So underneath our player collectible script and another field just going to pop up.

02:19.600 --> 02:24.680
And it will be current Apple Text as we name it the parameter or the variable.

02:25.240 --> 02:29.760
So what we need we need to drag this current Apple text into that slot.

02:29.760 --> 02:31.520
So we will have the reference.

02:31.520 --> 02:35.320
And after that we can link that up with our current apples.

02:35.680 --> 02:41.440
So now we can see underneath our player collectible collectible script it will be null like this.

02:41.480 --> 02:42.000
Empty.

02:42.040 --> 02:46.360
So what you need to do you need to drag the text component right over here.

02:46.800 --> 02:51.320
And we do know that if we select this current Apple text then go over to Inspector.

02:51.320 --> 02:57.200
We can see the text text component is sitting on its current Apple text game object.

02:57.200 --> 03:00.480
So that's why we need to drag this GameObject into that field.

03:00.520 --> 03:03.120
So we're going to select our player collectible script.

03:03.120 --> 03:07.920
And let's select this current Apple text and then drag it over to that slot.

03:07.920 --> 03:11.400
And now we can see we have the current Apple text reference.

03:11.400 --> 03:14.360
Basically this text component as you can see.

03:14.640 --> 03:17.000
And now we are ready to set that up.

03:17.160 --> 03:19.840
So what we're going to do we're going to open up our script.

03:20.440 --> 03:23.960
And we need to move over to in which line of code we doing.

03:23.960 --> 03:27.640
Basically collecting our apples right over here.

03:27.640 --> 03:31.400
You can notice we just increasing our current apples.

03:31.400 --> 03:34.120
So we also need to update that in the UI.

03:34.280 --> 03:39.120
So basically what we have to do we need to go over to this current Apple text game object first.

03:39.280 --> 03:41.800
And then we need to access this text.

03:41.800 --> 03:45.520
And we need to set this text equal to our current apples.

03:45.840 --> 03:49.720
And basically means how many apples we have right now.

03:49.960 --> 03:51.960
So let's go over to our script.

03:52.040 --> 03:57.200
So after collecting the apples we're going to move over to that current Apple text game object.

03:57.200 --> 03:59.600
And now we're going to access the text component.

03:59.600 --> 04:01.400
So by dot text.

04:01.400 --> 04:04.800
And we're going to set equal to our current Apple tax.

04:04.840 --> 04:06.240
Sorry, not current Apple.

04:06.840 --> 04:11.880
We basically need to set this current apples and close that up with semicolon.

04:12.040 --> 04:17.440
But the problem is that this one over here, as you can see it, this is an integer value.

04:17.440 --> 04:23.000
So if we go to top of our class we can see this current apples is a integer value.

04:23.040 --> 04:24.760
We can notice right over here.

04:24.920 --> 04:27.760
And this right over here is a string value.

04:27.880 --> 04:31.120
So we need to convert this integer value to a string.

04:31.120 --> 04:32.560
So how we can do that.

04:32.600 --> 04:36.200
We basically need to type after current apples dot two string.

04:36.200 --> 04:39.240
And this function just gonna convert a string value.

04:39.640 --> 04:39.920
Sorry.

04:39.960 --> 04:42.520
It's gonna convert integer value to a string.

04:42.520 --> 04:44.480
Now this is a string value.

04:44.480 --> 04:48.000
So we firstly going over to current Apple text game object.

04:48.000 --> 04:50.200
Then we accessing the text component.

04:50.200 --> 04:53.000
And we're setting equal to our current apples.

04:53.000 --> 04:55.920
So let's suppose this current apple something like five.

04:55.960 --> 04:59.920
Then this current apple text text right over here five.

04:59.960 --> 05:00.280
Why.

05:00.320 --> 05:02.880
Because we directly setting it as you can see.

05:03.160 --> 05:07.000
So firstly we're just going over to this current Apple text game object.

05:07.240 --> 05:12.320
Then we accessing this text and we're setting this text equal to our current apples.

05:12.360 --> 05:14.560
As you can see right over here.

05:14.560 --> 05:17.080
So that's why it's going to update in the UI.

05:17.360 --> 05:21.840
And we're going to press down Ctrl s on our keyboard in order to save our script.

05:21.840 --> 05:25.800
And make sure you just move over to unity and check that out.

05:26.000 --> 05:31.800
So as soon as we're going to collect the first apple, we will be able to notice our current Apple text

05:31.800 --> 05:32.840
gonna be one.

05:32.840 --> 05:35.960
And if we collect second Apple, then it's gonna be two.

05:35.960 --> 05:37.120
And so on.

05:37.400 --> 05:40.040
So you can check with many apples you want.

05:40.080 --> 05:42.000
I'm gonna duplicate this apple.

05:42.040 --> 05:46.800
So I will move over to hierarchy and let's go to.

05:47.920 --> 05:48.520
Sorry.

05:48.560 --> 05:50.800
We, uh, have the Apple.

05:51.040 --> 05:53.600
So if collectibles right over here.

05:53.600 --> 05:57.840
So I'm going to select this heart and let's press down Ctrl D to duplicate.

05:57.840 --> 06:00.880
And we're going to press down F in order to focus on it.

06:00.880 --> 06:03.080
And let's select the move tool in the scene view.

06:03.520 --> 06:06.320
Sorry we duplicated this heart.

06:06.320 --> 06:07.680
So I'm going to delete that.

06:07.680 --> 06:09.960
I'm going to select the apple in the hierarchy.

06:10.120 --> 06:12.640
And let's press down Ctrl D to duplicate.

06:12.640 --> 06:15.400
And let's move it over somewhere around here.

06:15.800 --> 06:17.880
And I'm gonna duplicate one more time.

06:17.880 --> 06:19.240
Let's move it here.

06:19.240 --> 06:21.120
So we have three apples.

06:21.120 --> 06:27.360
So as soon as we're gonna collect the first one our current apple is gonna be one.

06:27.360 --> 06:30.280
And if we collect the second one, then it's gonna be two.

06:30.320 --> 06:33.320
And if we collect the third one, then it's gonna be three.

06:33.520 --> 06:34.960
So let's check that out.

06:34.960 --> 06:39.520
So I'm going to click on this play button in the top in order to play our game.

06:39.520 --> 06:41.520
And let's take a look on this.

06:41.520 --> 06:46.400
So as soon as we're gonna collect the first one our current Apple text will be one.

06:46.680 --> 06:49.760
And if we collect the second one then it's gonna be two.

06:50.200 --> 06:52.040
So if we collect the first one.

06:52.040 --> 06:53.800
So let's try to collect this.

06:53.800 --> 06:59.120
As you can see, as soon as we collect it, this current apple takes right over here.

06:59.120 --> 07:05.480
If I select my canvas in the hierarchy and press down F in order to focus on it, we can notice that

07:05.480 --> 07:08.920
this current Apple tax is now one.

07:08.920 --> 07:13.160
And if we try to collect the second one now we can see it's now two.

07:13.360 --> 07:16.360
And if we collect the third one then it should be three.

07:16.360 --> 07:21.320
So if we sorry if we collect the third we can see now it's became three.

07:21.600 --> 07:24.400
So I hope you get the idea that how you can do it.

07:24.400 --> 07:27.840
And we're gonna do the same thing as well with our heart.

07:27.840 --> 07:31.040
So let's get out of play mode and let's do the same thing.

07:31.040 --> 07:37.160
So firstly, what we need if we move over to our hierarchy canvas and then UI game object and select

07:37.160 --> 07:43.000
this heart text, we can see that the text component is setting on this heart text game object.

07:43.000 --> 07:45.920
So we need a reference of this text component.

07:45.920 --> 07:50.280
And after that we can just link that up with our player Maxell.

07:50.320 --> 07:56.000
So we're gonna open up our script and let's go to top of our class and let's grab the reference.

07:56.000 --> 07:59.280
So we're gonna make another serialized field private.

07:59.280 --> 08:01.840
And the component we're looking for is a text type.

08:01.840 --> 08:08.390
So we're going to type this text and we're going to call this one current, uh, current, uh, let's

08:08.390 --> 08:12.110
call it current health, uh, health text, something like that.

08:12.110 --> 08:17.390
And we're going to close that up with semicolon and make sure once you press down Ctrl S, then move

08:17.390 --> 08:22.830
over to unity underneath your, uh, player collectible script, you're gonna able to notice that,

08:22.830 --> 08:30.150
uh, sorry if I select my, uh, player GameObject in the hierarchy, and I need your player collectible

08:30.150 --> 08:33.750
script or wherever you just made it, you're gonna see that fill.

08:33.750 --> 08:35.750
So make sure you drag the reference.

08:35.950 --> 08:40.390
So after grabbing the reference now what we need to do, we need to set that up.

08:40.510 --> 08:43.870
And we're going to set this up with our update function.

08:43.870 --> 08:47.310
And also we need a reference of our player's game.

08:47.310 --> 08:52.030
So if I select my player game object then go over to Inspector then player skip.

08:52.070 --> 08:56.150
We can see inside this player script we have the max health.

08:56.190 --> 08:58.830
So we need a reference of this player skip.

08:58.830 --> 09:00.750
Then we can just grab the reference.

09:00.750 --> 09:02.910
So make sure you just grab the reference.

09:02.910 --> 09:05.790
And we do know that we already done it.

09:05.790 --> 09:08.950
So if I open up my player collectible script.

09:08.950 --> 09:13.310
As you can see, we already grabbed the reference in the start function.

09:13.310 --> 09:18.070
As you can notice, firstly, we just created a variable for our player script.

09:18.070 --> 09:20.310
As you can see private player player.

09:20.550 --> 09:25.590
And then we just moving over to start function and we're just taking our player script.

09:25.590 --> 09:29.710
As you can notice this dot game object basically referencing itself.

09:29.910 --> 09:36.110
And we're just getting the player script component or player script and we're storing into this player

09:36.110 --> 09:37.350
as you can notice.

09:37.350 --> 09:38.910
So we already done it.

09:38.910 --> 09:42.070
And now what we have to do, we need to update in the UI.

09:42.230 --> 09:45.270
To do that we're going to move over to this update function.

09:45.270 --> 09:49.030
So update function runs every single frame and every single frame.

09:49.030 --> 09:52.550
We need to update the health in the UI text.

09:52.550 --> 09:56.630
So what we're going to do now we are gonna do this thing.

09:56.670 --> 10:00.150
Firstly we will go to current uh health text.

10:00.190 --> 10:02.070
And then we're going to access the text.

10:02.070 --> 10:06.910
And we're gonna set equal to our player Dot Max hell.

10:06.950 --> 10:09.270
And we're going to close that up with semicolon.

10:09.510 --> 10:14.510
So we're getting the same error, which is uh, this is integer value right over here.

10:14.510 --> 10:16.670
So if I move over to my player script.

10:16.790 --> 10:21.590
So we basically accessing this player and we sorry accessing this max cell.

10:21.590 --> 10:25.110
And as you can notice this max cell is a int parameter.

10:25.110 --> 10:25.910
We can see.

10:26.270 --> 10:32.750
So so what we're doing simply over here we're just going over to their player max cell.

10:32.870 --> 10:34.630
So this is integer value.

10:34.670 --> 10:37.790
Now we need to convert this into a string value.

10:37.790 --> 10:42.510
To convert that we need to type this dot to string and then make this first bracket.

10:42.670 --> 10:45.510
So firstly we just going over to current text.

10:45.550 --> 10:48.710
Basically we're just going over to this UI.

10:48.750 --> 10:52.190
Then this heart text heart text game object.

10:52.190 --> 10:55.350
And we accessing the text component in Inspector.

10:55.350 --> 10:56.510
As you can notice.

10:56.510 --> 11:00.550
And we setting this text component equal to our player max cell.

11:00.550 --> 11:03.670
And let's suppose player max is something like three.

11:03.710 --> 11:08.870
Then this right over here it's gonna be three because we directly setting it, as you can notice.

11:09.350 --> 11:12.350
Uh, really sorry.

11:12.750 --> 11:18.630
So firstly we going over to current health text and we accessing the text component.

11:18.630 --> 11:21.430
And we setting equal to our player Max health.

11:21.510 --> 11:25.590
So the text will be changed according to our player max health setting.

11:25.870 --> 11:31.430
Since we just directly setting it or we just setting it equal to this as you can notice.

11:31.590 --> 11:36.590
And now we're going to press down Ctrl s on our keyboard in order to save our script.

11:36.590 --> 11:38.550
And now let's move over to unity.

11:38.750 --> 11:45.350
So underneath our player collectible script we will able to notice another field just going to pop up

11:45.350 --> 11:48.190
which is current uh health text.

11:48.190 --> 11:53.990
So what we need to do there, we basically need to drag this text game object into that field.

11:53.990 --> 11:58.910
Since the text component is sitting on inside this heart text game object.

11:58.950 --> 12:01.990
Now we're going to select our player game object in the hierarchy.

12:02.190 --> 12:03.910
And let's select this player script.

12:03.950 --> 12:09.910
As you can notice underneath our player collectible script we able to notice a current health text and

12:09.910 --> 12:11.870
it says none basically empty.

12:11.870 --> 12:15.070
So we're going to take our health text and drag it over there.

12:15.110 --> 12:16.590
Now we have the reference.

12:16.590 --> 12:20.030
And now we're going to be able to check it in key.

12:20.230 --> 12:25.110
So if I just go ahead and then click on this play button in the top, you will notice that.

12:25.390 --> 12:29.070
So as soon as we're going to collect the this heart the first.

12:29.070 --> 12:31.150
But we're not gonna able to do it.

12:31.190 --> 12:31.670
Why.

12:31.990 --> 12:38.510
Because the if the max health of this uh player game object is already five, then we're not taking.

12:38.510 --> 12:44.990
So as you can see, as soon as the game started, we can see player Max health is five just updated

12:44.990 --> 12:45.790
in the UI.

12:45.830 --> 12:46.310
Why?

12:46.510 --> 12:52.790
Because if I select my player GameObject in the hierarchy, then go over to Inspector, then player

12:52.790 --> 12:53.270
Skip.

12:53.470 --> 12:58.910
Because in the start of the game we setting player max which is five as you can notice.

12:58.910 --> 13:05.070
So that's why this text right over here, it's became five because, uh, we do know that inside our

13:05.070 --> 13:09.470
skip, we just directly setting it, as you can see right over here.

13:09.670 --> 13:16.110
So now if I drag down a little bit, basically if I make, I make the max health of this player game

13:16.150 --> 13:17.630
object something like four.

13:18.070 --> 13:23.630
But before we do that, if we try to collect our heart, we not able to do it.

13:23.630 --> 13:29.310
So because we already have the max health is five and we're checking if the max health of this player

13:29.350 --> 13:33.310
game object is already five, then we're not collecting at all.

13:33.310 --> 13:35.230
So let's make it something like four.

13:35.270 --> 13:38.030
So I'm going to select my player game object in the hierarchy.

13:38.230 --> 13:41.470
Let's go over to Inspector then player Skip and the max.

13:41.470 --> 13:43.550
Well I'm gonna make it something like four.

13:43.750 --> 13:45.910
Now as soon as I just type four.

13:45.950 --> 13:49.590
It just updated in the UI because we directly setting it.

13:49.590 --> 13:52.030
And now we will able to collect this.

13:52.030 --> 13:57.110
So if I collect this heart, as you can see, as soon as we collect it, it disappeared.

13:57.110 --> 14:00.230
And we can see the max health of this player game objects.

14:00.270 --> 14:01.790
Now it's became five.

14:02.070 --> 14:07.550
So I hope you get the idea that how you can link up your UI text with script.

14:07.550 --> 14:09.350
So I'm gonna get out of play mode.
