WEBVTT

00:00.000 --> 00:00.420
Hello guys.

00:00.420 --> 00:01.350
Welcome to the video.

00:01.350 --> 00:03.690
In this one I want to do something with the fruits.

00:03.690 --> 00:08.940
So later on in the game we're going to have two stats while we completing the level.

00:08.940 --> 00:14.220
One of them will be how many fruits are collected and how many fruits total are there.

00:14.220 --> 00:17.250
And that's what I want to do in this video.

00:17.250 --> 00:21.930
And later on, once we build the UI, we can just take the information that we make in this video and

00:21.930 --> 00:23.580
just display it in the UI.

00:23.610 --> 00:24.510
Simple as that.

00:24.510 --> 00:27.150
Also, it is very easy to make so.

00:28.550 --> 00:31.460
Let's go to game manager over here.

00:31.460 --> 00:36.710
First of all, I want to rename this boolean because it's too long I'm going to type.

00:36.710 --> 00:39.170
Fruits are random.

00:39.530 --> 00:40.520
It's a bit better.

00:40.910 --> 00:42.860
And we need public.

00:45.220 --> 00:45.790
End.

00:46.630 --> 00:50.680
Total fruits like total fruits amount.

00:51.160 --> 00:53.500
Now we need to know how many fruits are there.

00:53.500 --> 00:56.320
And of course we could manually assign the number.

00:56.320 --> 00:58.150
Let me do close all but this.

00:58.450 --> 01:01.870
But that is not the way programming is working, right?

01:01.870 --> 01:07.150
We don't need to do everything manually except if we change amount of fruits on the level, we may forget

01:07.150 --> 01:08.170
to update this value.

01:08.170 --> 01:11.860
So I want to update this value in the script.

01:11.860 --> 01:19.630
Every time we start the game, let's open start method and we can make a local variable of array.

01:19.810 --> 01:22.690
Or probably I did not tell you about array before.

01:23.500 --> 01:25.960
Okay, first let me explain.

01:26.860 --> 01:28.150
Let me make public.

01:29.100 --> 01:29.580
Route.

01:30.120 --> 01:31.050
Uh, variable.

01:31.050 --> 01:36.540
And we're going to put these two square brackets, which makes it an array, sort of like a list but

01:36.540 --> 01:37.170
an array.

01:37.170 --> 01:39.660
And let's do all fruits.

01:41.400 --> 01:44.820
Let's save the script and let's go back to unity.

01:49.360 --> 01:55.450
Now we've got a list over here and we can assign fruits we have just like that.

01:56.260 --> 02:00.100
Now we have an array of the fruits we have on the level.

02:00.310 --> 02:08.920
And at the start, we can go back here and say that total fruits equals to all fruits.

02:08.920 --> 02:12.160
Dot lengths and lengths is the length of the array.

02:12.760 --> 02:18.130
If array has three elements then this value will be equals to three.

02:20.030 --> 02:20.810
You can see that.

02:20.810 --> 02:21.440
Okay.

02:21.440 --> 02:22.430
Very cool.

02:23.380 --> 02:27.850
However, that's not all of it because I want to make it work automatically.

02:27.850 --> 02:34.420
We can select objects with a component of a fruit into the array in the script.

02:34.420 --> 02:34.810
This.

02:34.810 --> 02:35.530
What are we going to do?

02:35.530 --> 02:39.790
All fruits equals to find objects of type.

02:41.340 --> 02:41.940
Route.

02:43.780 --> 02:44.320
Oh.

02:44.650 --> 02:45.790
What's up?

02:46.750 --> 02:49.570
Or we need to type find objects with a s.

02:50.250 --> 02:52.230
And still it has an error.

02:53.490 --> 02:54.540
Okay.

02:54.540 --> 02:58.200
They changed it in a unity 2023.

02:58.380 --> 02:59.610
Okay I see.

02:59.640 --> 03:03.540
So if you have unity 2023 we're going to write it differently.

03:03.540 --> 03:06.600
If you have unity 2022, this is how you can do it.

03:07.630 --> 03:10.480
But since I'm using 2023, let's do it differently.

03:10.480 --> 03:17.230
I think we can type over here, find objects by type and in the parentheses we need to type.

03:17.560 --> 03:22.060
This is new for me a sort mode none.

03:22.570 --> 03:24.310
So we don't have to sort it out.

03:24.310 --> 03:26.020
Now we can go back to unity.

03:26.020 --> 03:27.940
And I'm going to show you how it's going to work.

03:28.510 --> 03:33.520
And basically this will find all of the objects that are having a component of a fruit.

03:33.520 --> 03:36.550
And it's going to put it into the array that I showed you before.

03:38.230 --> 03:39.760
Let me clean the array.

03:39.760 --> 03:41.530
I'm going to make it empty.

03:43.550 --> 03:46.130
And I'm going to add another fruit on the level.

03:47.730 --> 03:53.520
So now we have four, and I'm supposed to have an array filled with new fruits, all four of them.

03:53.520 --> 03:56.280
And this should say for let's go to play mode.

03:58.290 --> 04:01.560
And we've got an array and this says four.

04:01.590 --> 04:02.280
Okay.

04:02.280 --> 04:03.300
Very cool.

04:03.330 --> 04:08.760
Now again we don't have a reason to keep this variable over here because we just need to count fruits

04:08.760 --> 04:09.660
on the level.

04:09.660 --> 04:13.320
Because of that I can remove this variable over here.

04:13.320 --> 04:18.870
And I can make a local variable root array just like that.

04:18.870 --> 04:21.420
And also we can wrap this into a method.

04:21.420 --> 04:23.430
So I'm going to press control dot.

04:24.300 --> 04:25.920
Extract method.

04:28.030 --> 04:30.670
Collect fruits info.

04:31.270 --> 04:32.470
Maybe just like that.

04:33.250 --> 04:34.120
Very nice.

04:34.120 --> 04:40.990
So now later on, once we have UI, we can display total amount of fruits in the corner of the screen

04:41.170 --> 04:42.190
over here.

04:42.190 --> 04:45.640
And we can just update the value of fruits that we picked up before.
