Hello guys.
Welcome to the video.
In this one I want to do something with the fruits.
So later on in the game we're going to have two stats while we completing the level.
One of them will be how many fruits are collected and how many fruits total are there.
And that's what I want to do in this video.
And later on, once we build the UI, we can just take the information that we make in this video and
just display it in the UI.
Simple as that.
Also, it is very easy to make so.
Let's go to game manager over here.
First of all, I want to rename this boolean because it's too long I'm going to type.
Fruits are random.
It's a bit better.
And we need public.
End.
Total fruits like total fruits amount.
Now we need to know how many fruits are there.
And of course we could manually assign the number.
Let me do close all but this.
But that is not the way programming is working, right?
We don't need to do everything manually except if we change amount of fruits on the level, we may forget
to update this value.
So I want to update this value in the script.
Every time we start the game, let's open start method and we can make a local variable of array.
Or probably I did not tell you about array before.
Okay, first let me explain.
Let me make public.
Route.
Uh, variable.
And we're going to put these two square brackets, which makes it an array, sort of like a list but
an array.
And let's do all fruits.
Let's save the script and let's go back to unity.
Now we've got a list over here and we can assign fruits we have just like that.
Now we have an array of the fruits we have on the level.
And at the start, we can go back here and say that total fruits equals to all fruits.
Dot lengths and lengths is the length of the array.
If array has three elements then this value will be equals to three.
You can see that.
Okay.
Very cool.
However, that's not all of it because I want to make it work automatically.
We can select objects with a component of a fruit into the array in the script.
This.
What are we going to do?
All fruits equals to find objects of type.
Route.
Oh.
What's up?
Or we need to type find objects with a s.
And still it has an error.
Okay.
They changed it in a unity 2023.
Okay I see.
So if you have unity 2023 we're going to write it differently.
If you have unity 2022, this is how you can do it.
But since I'm using 2023, let's do it differently.
I think we can type over here, find objects by type and in the parentheses we need to type.
This is new for me a sort mode none.
So we don't have to sort it out.
Now we can go back to unity.
And I'm going to show you how it's going to work.
And basically this will find all of the objects that are having a component of a fruit.
And it's going to put it into the array that I showed you before.
Let me clean the array.
I'm going to make it empty.
And I'm going to add another fruit on the level.
So now we have four, and I'm supposed to have an array filled with new fruits, all four of them.
And this should say for let's go to play mode.
And we've got an array and this says four.
Okay.
Very cool.
Now again we don't have a reason to keep this variable over here because we just need to count fruits
on the level.
Because of that I can remove this variable over here.
And I can make a local variable root array just like that.
And also we can wrap this into a method.
So I'm going to press control dot.
Extract method.
Collect fruits info.
Maybe just like that.
Very nice.
So now later on, once we have UI, we can display total amount of fruits in the corner of the screen
over here.
And we can just update the value of fruits that we picked up before.