Hello guys.
Welcome to the video.
So now we know how Ontriggerenter works and we're going to use it to make collectable items.
Let's go to the graphic items.
And over here we have fruits.
Let's get one of the fruits.
Actually we need to select all of them and make pixel per unit 16.
And then we're going to work with one of them.
And after that I'm going to show you how to apply different skin to different fruits.
For starters let's just work with one of them.
Let's set certain layer to items and let's name it as can we?
Yeah, let's keep visuals as a child object.
So let's call it animator and let's make an empty parent.
I'm going to call it fruit.
Very well.
Now let's add component new script.
And I'm going to call it fruit.
But if you have coins in your game or something like that, you can just name it as coin or you know,
anything you like.
So let's create and add this component.
Now let's take the script and drag it inside of a script folder so it's cleaner.
And after that we can go inside of this fruit.
And first, what we need to do is to get access to the game manager, because now we don't have any
other objects.
So I want to keep score of the fruits we have on the game manager.
Again, we can use an instance, but also we can create a variable and assign it through the instance.
This is how it works.
We're going to do private.
Game manager.
Then I want to do this in the start, not in the awake.
Let's do game manager equals to game manager dot instance.
I'm doing this in the start and not in the awake because uh, game manager is assigned in the awake.
And if we get component in the awake as well, it may happen that awake of this script will be called
before the game manager.
And because it will be called before this, awake will not have time to assign the instance, and it's
going to be null when we train to get it.
Okay.
And now let's go to Game Manager and let's make, uh, public int fruits collected.
And I want to make a public.
Void add route and this method will do one simple thing.
It's going to do fruits collected plus plus when we type in plus plus just like that, it means plus
one.
And the same thing would be if we would type fruits collected plus one the very same.
But it is easier that way.
So let's do plus plus okay.
Now on the fruit we're going to do on trigger enter to D.
And then we're going to check player player getcomponent of a player.
If player not equals to null then game manager and route just like that.
Okay.
That's all what we need to do for the script.
Now let's go back here.
Let's add a circle.
Collider.
Circle.
Collider 2D.
Here it is, I think.
Yeah, should be fine.
It's fine if Circle Collider is a bit bigger because we need to make it easy for the player.
In fact, we can make it even bigger.
Let's make it 0.75.
Maybe so player will pick it up.
Uh, anyway.
Nice.
Now let's select this trigger over here.
And now we need to play animation for the fruit.
So we're going to go to Assets Animations.
And let's make folder for all of these folder layer.
We're going to drag it like so.
And then let's make.
Just animate our controller this time.
Root AC root.
Animator.
Controller.
Now let's put it on the animator over here.
Very nice.
Now we need to get reference to this controller.
We can go to the script over here.
And make.
Private animator.
Annum and then we can do annum equals to get component of the animator.
Actually would be better to do it in their wake, just in case.
So let's do it like so.
I know it looks a bit extra that we have two methods where we're getting different things, but let's
keep it that way.
It's just safer that way.
Now we have animator.
Are we going to need it in the future?
Right.
So let's just keep it at that and let's go to the graphics folder and get some animation for the animator
I'm going to go to graphics items fruits.
These are the fruits we're going to use.
I'm going to select animator of the animator.
Go to animation and create new clip.
In the animations.
I'm going to make a fruit apple.
Let's save this.
Let's take sprites of an apple and just drag it like so.
Also, I learned that it's better to set sample array 20 looks better.
So we're going to change sample rate for everything but a bit later.
As of now let's just do it for the fruit.
So this looks good I like it.
Very nice.
Um, there's going to be default animation, so it's going to be played anyway.
I just want to see how it all worked out.
Let's take the fruit.
Let's place it somewhere here and let's go to play mode.
So now we have a fruit.
It is.
They're bouncing.
I'm going to go there and pick it up.
I did not pick it up.
Why not?
Oh, I did actually.
I'm sorry.
We did pick up the fruit.
It is two, three, four, five.
We did not destroy the fruit.
That's the problem.
Let's go to the script and we're going to open curly brackets and do destroy game object.
So it's going to be destroyed as well.
Very nice.
Let's go back.
And boom.
I have one fruit on the game manager over here.
Very cool.
Hello guys.
I've noticed a mistake over here in the wake.
It should be getcomponent in children, not getcomponent because animator is a child of a fruit GameObject.
All right, that's good.
Thank you so much for paying attention and I'll see you in the next video.