Hello guys.
Welcome to the video.
In this one I want to show you something very important.
I want to show you how to create objects at runtime.
This is very important feature that can help us to respawn player when he dies, or maybe respawn enemies
if you want to, or some you know, other things that may be needed in your game in your project.
So to explain how do we create an object?
I want to create a visual effect for the fruit when we picking up the item.
Let's go ahead to the graphics.
Items fruits.
And there is this collected visual effect.
Let's take the first sprite and let's drag it over here I can see we need to do 16.
It's okay.
So we have this.
Let's set it on a items layer and let's do minus one over here.
So it will be behind the fruit itself.
If we set order in layer zero it will always depend on the hierarchy.
If we set it to one it will be always above the fruit.
But we want it to be behind the fruit.
So I'm going to do minus one.
Very well.
Now, uh, I think we do need to make a child object for this one because we're just going to have visual
effect, nothing else.
So we can place animator on the same object.
Let's just name it as pickup.
We fix.
So this is a visual effect for the item pickup.
And let's add an animator.
Uh, controller?
Now we need a controller over here.
So I'm going to go to Assets Animation and create animator controller.
Pick up the fix.
AC okay, now let's drag it on the visual effect over here.
So this is the thing.
That's the only thing we need for now.
Now let's go.
Uh, to the animation, click Create New Clip and we're going to do.
Fruit.
Pick up my fix.
Okay, nice.
And now we need to go back to graphics and grab those sprites.
And put them on the animation.
Sample rate will be 20.
And this is how it looks.
Nice, right?
So this is how it looks when we pick it up the item.
Uh, we also would have to make it transparent once it is done.
Like once we don't have anything to display once animation is over.
And we could disable this visual effect right after the last frame.
But for that, we would have to make a script, which I don't really want to do, because that would
be just for one.
Actually, it would be a good idea because I can show you how to use animation events.
So let's do it.
First, let's create the visual effect.
After we pick up the fruit for that we need to make it as a prefab.
So I'm going to go to prefab folder.
And I'm going to drag this visual effect over here.
Now whenever we want to use a visual effect we can just create this object at runtime.
This is how we do it.
We go to the fruit apple.
Let's go to the script.
Actually, it can be any fruit.
I'm just choosing this one.
And over here we need serialize field.
Private GameObject.
Pick up very fix very well.
So now we have a reference to the object that would serve as a visual effect.
Now once we are.
Done with the fruit.
Once we destroy in it, we can create a visual effect.
For that, we can use instantiate.
And usually I like to get a local variable of the game object in case we need to do some manipulations
with it.
So I'm going to do game object.
New fix equals to instantiate.
Pick up a fix so this one will create object at runtime.
Then if you need to do something with it, you can do a new fix and do some stuff.
For example, I want to destroy this effect after one second.
So I'm going to do destroy a new way fix, and I'm going to give it a delay of maybe even 0.5 seconds.
So that's it.
So now this will create an object.
And I think we can go ahead and just see how does this work.
Let's go to unity.
Now we would have to assign this visual effect to all of the prefabs we have.
So I'm going to go to prefab folder.
Select all of the fruits and just drag this effect over here.
Very well.
Now these all have in their visual effect.
And I'm going to go ahead and try to pick up the items.
Now we can see what is happening there if you don't destroy the object.
So I'm going to just switch it off for now.
And let's go.
And we created a visual effect.
The only problem is it is created over here.
Not at the position of the fruit.
To fix that, we need to go to the script.
And over here in the parenthesis we can pass a transform position.
And because we type transform position over here, it will take position of the fruit.
Because we instantiate an object inside of a fruit script.
So transform position of a fruit script is a position of the object right position of the fruit.
So we type here transform position.
And now we need to pass here a rotation.
That is how it's supposed to be.
So I'm going to type quaternion identity which basically means do not change the rotation.
This is how it is.
So I'm going to save this.
Going to go back.
Go to play mode and I'm going to pick up the.
Let me switch off this one.
I'm going to pick up the fruit and we can see visual effect was created and the fruit itself.
The only issue is it was played a couple of times.
Here.
We have a couple of ways to solve it.
One of them we can go to asset.
Animations, find the pickup animation through the pickup wave fix, and we can uncheck loop time over
here.
Because of that, it will be played only once.
Let's go ahead.
I'm going to show you how does it look.
So here it is.
It will stop at the last frame, and we could find an empty sprite somewhere and put it in the end of
the animation so it will become invisible once visual effect is played.
That is one of the ways to do it.
Let me go ahead and quickly find some empty sprite I think we can just go to.
Let's go to items.
Fruits.
Let's take this collected.
I'm going to duplicate it.
Let's just do copy paste.
I'm going to name it as empty.
And in the sprite editor over here, instead of all of this, we can simply.
Take this area where we have nothing.
So now we're going to hit apply.
This will be an empty sprite over here, and we can go to the end of the animation and place an empty
sprite over here.
So now it will play the animation and then it will become invisible.
And then we can destroy it whenever we are ready.
And this is how it would look.
Yeah, and this works pretty well.
And to be honest, if I would just be making projects for myself, I would do it this way because I
kind of like how tricky it is.
We don't need to do anything extra, but this is a great chance for me to show you how to work with
animation events, and that's what I'm going to do.
Let's go to the scripts and let's make.
Uh C sharp script.
Road.
Pick up the fix.
Just like that.
And over here we're going to make a method public void destroy me, in which we're going to just type
destroy game object just like that.
That's all that we need.
And then if we go to pick up visual effect over here and put the script.
On the GameObject over here.
Then in the animation over here, we can go to the frame after the last frame over here.
And over here we can click Add Event.
We added an event and over here for this event we can choose function.
And because we have this fruit pickup visual effect script, we can go to methods over here and find
Destroy Me method that we just created.
This can be useful for many cases.
This is just an example on how to use events.
This function can say anything.
You can update the variables.
You know so many things can be done here.
So we're going to assign this method over here.
And because of that this method will be called exactly at this frame.
And because of that fruits will be destroyed even if we don't set timer.
Uh, over here, let me just set five for example.
So it should be destroyed after five seconds, but you'll see it will be destroyed once animation is
over.
And there it goes.
Works like a charm.
Yeah, we need to.
We need to do apply overrides to everything.
So this, uh, change will be applied to all of the visual effects, even in the prefab folder over
here, because this one had none.
So over here we can see we have an event and it is assigned okay.
Now let's go back.
We can delete this one.
We can clear the console and let's go to game window.
And I'm going to pick up fruit and visual effect played and destroyed.
So I showed you two approaches.
How we can do it.
Which one to choose is up to you.
I just want to teach you as many things as possible.
All right.
For educational purpose, I'm going to keep this script in the script folder.
Let it be there and let's use animation event because it doesn't really matter what we choose here.
But you should know that we could just use a delay over here and empty sprite in the end of the animation.
Yeah, well, for now, I'm just going to remove it.
Like so and I guess that's it.
So this is how you use instantiate.
And we're going to use it to respawn the player in a couple of videos.
So yeah, I guess this is it.
Thank you so much for paying attention.
And I'll see you in the next video.