We will create a game manager script and that script.
Just going to keep track of this player game object is active or not.
But before we do, let's open up the player script and we're going to destroy this player player game
object once it loses all of his health.
So let's select the player game object in the hierarchy, and let's open up the player script by double
clicking on it.
And let's go to bottom basically two die function.
As you can see, we only just printing out the message in the console.
Player died.
And after that we also need to destroy our player game object.
Basically it loses all of his health.
So that's why.
So we're going to use this destroy function in order to do that.
So going to use this destroy destroy and this over here as a first argument.
It's going to take the game object you want to destroy.
So we're going to pass this game object.
It's basically mean just it just referencing itself and comma.
If you just want to specify the time after which amount of time you want to destroy.
Then as a second argument, you can pass that.
But I don't want any delay.
I just want to destroy it right away.
So that's why I'm gonna pass this date, this game object only.
And after that, we're going to close that up with semicolon.
And let's press down Ctrl S to save our script.
And now let's move over to unity.
So as soon as this player game object is going to lose all of his health, you will see this game object
will destroy right away.
So just wait for to compile.
It's gonna take a few moments and once it's done, we are ready to do that.
So now, uh, if we click on this play button in the top, you will see as soon as this player game
object is just gonna lose all of his health, it will destroy itself right away.
But if you want to delay, then you can just put as a second argument the time as well.
But, uh, but let's have a look on this.
Once this player game object loses, then as you can see, four, three, two, one.
As you can see, another player just died right away.
But the problem is that camera is still moving to the right direction.
Once our player game object just dies, then we just want to stop this camera to move to the right direction.
So that's why we're going to create a script in order to do it.
Basically, we're gonna create a game manager script, and it's just going to keep track of if the game
is active or not, or the player game object is still alive or not.
So let's get out of play mode and let's create that.
So I'm going to move over to project tab then assets and then scripts folder.
Let's right click there then create and then Monobehaviour script and name that script to something
like Game Manager.
I already created a Game Manager strip over here, as you can see.
So we can simply attach this script to any game object of our hierarchy, or we can simply create an
another GameObject for this script.
So let's do that.
Let's right click or hit this plus icon.
Let's create an empty game object.
And I'm gonna name this GameObject to something like Game Manager.
So let's call it Game Manager.
And let's go to transform.
Let's right click there reset.
And I'm going to select my Gamemanager script.
And I'm going to drag it top up in my hierarchy so I can see all the time.
And let's select the Game Manager script and let's drag it over to this Add Component section.
And in the Inspector.
So now we're going to open up our player this Game Manager script by double clicking on it.
And I'm going to remove this start and update function I don't need.
Instead we're going to make a parameter.
And we're going to make that parameter to public because we're going to access this parameter through
another class or another, uh, basically another uh script you can say.
And that will be bool, which can be true or false.
Uh, basically this bool parameter over here.
It's just going to keep track of if the game is active or the player game object is alive or not.
And I'm gonna call this one is game active is game Active.
And let's close that up with semicolon.
And I'm going to use this start function.
So let's include the start function.
So this start function get called in the start of the game.
So in the start of the game I'm going to set.
This is a is game active parameter to true.
Basically once our game start is is game active parameter should be true.
But once our player game object destroy then we're gonna set the is game active parameter to false.
So I'm going to use this is game active.
And we're going to set equal to false in the start of the game.
And we're going to create a singleton pattern to just access this game manager script through another
class or this parameter in another class simply.
Uh, so if you haven't watched the singleton lecture then you can go and check that out.
Or you can simply use find any object by type, and you can simply pass the script name and you can
access the parameter.
But instead of doing fairly taxing it, basically it's fairly taxing and it's heavy text texting.
So that's why we're going to use this singleton pattern.
So we're gonna use this public static static.
And we're going to create a parameter as the same name as same name of our script or class.
And I'm going to call this one instance instance.
Let's close that up with semicolon.
And let's move over to start function.
And over here I'm going to do I'm going to check if instance equal to equals equals to null.
Basically means there is no instance of this a game manager.
Then I'm going to set instance equal to this.
And if there is already an instance then I'm going to destroy this, uh, player script or this instance.
So I'm going to say uh, we're going to use this destroy function.
And I'm going to say this, dot this, and let's close that up with semicolon.
Uh, if you a little bit confusing, then you can simply use find any object of type.
Then make arrow brackets.
And then in that arrow brackets you can pass the script name as we have done in the start of the game.
Basically, once we started coding we just use that, find any object by type.
And if you want to learn about more, uh, singleton pattern, then you can check out the Singleton
pattern lecture in my course.
And now I'm going to press Ctrl S to save our script.
And let's move over to unity.
So we're going to press down Ctrl S.
And we're going to open up our player script.
Uh basically where the player is dying.
So let's select the player game object in the hierarchy.
And let's open up the player script by double clicking on it.
So as you can see, player game object is dying over here.
Or we simply destroying our player game object right away.
So before we destroy, I'm going to set this game active parameter in our Gamemanager script to false
because our player game object totally get destroyed.
So that's why.
So before we die, before it's died, we're going to set this parameter to false.
So we can simply use this.
Find any object by type.
And in this arrows you have to pass the name of the script or the script that you're looking for.
So in our case Gamemanager then make first packet and then use this dot.
And we're going to say is game active.
We're going to access that parameter.
And we're going to set it equal to false before we die.
But instead of using this we're going to use the singleton pattern as we created.
So we can simply pass the script name which is our gamemanager.
Then we will move over to instant.
And then we're going to access the is game active parameter.
And we will set equal to false.
So let's close that up with semicolon.
So we're just setting equal to false as you can see.
So basically what we're doing, we're just moving over to this Gamemanager script and then accessing
this scheme active parameter or this bool parameter as you can see.
And by accessing and resetting this scheme active parameter to false.
As you can see uh we just setting it equal to false.
But one when we're just doing that.
Once our player game object loses all of its health, as you can see, we're doing inside the die function
and we're calling the die function.
Once our player game object loses all of his health before it loses all of his health.
Firstly, we just printing out a message in the console, says player died, and after that we're just
going over to game Manager script.
And then accessing that is game active parameter.
And then we're just setting equal to false.
And after that we're just destroying our player game object right away.
As you can see now I'm gonna press down Ctrl s to save my script and let's move to unity.
So as soon as our player game objects die, you will see that his is game active parameter just going
to be false right away.
And we can see underneath our game manager script as well, uh, in our Gamemanager game object.
So if I select my Game manager uh, GameObject in the hierarchy, we can see underneath our Game Manager
script we have an active parameter.
And by default it's basically unchecked basically means false.
But as soon as we're going to start our game, you will see this in-game active parameter.
Just gonna be, uh, true right away, because in the start of the game we are setting this is game
active parameter to true.
So that's why it's going to be checked.
So as you can see as soon as the game start is game active parameter is now true.
But once our player game object loses all of his health, you will see this game active parameter just
gonna be false.
As you can see, as soon as our player game object dies, this is game active parameter gets false because
once our player game object dies, we just setting this parameter to false.
So let's get out of play mode.
So we're going to use this is game parameter in order to move our main camera.
So if it's game active parameter is false.
Basically means our player game object dies means we need to stop moving this main camera.
So let's open up the follow script where we simply moving our camera.
So I'm going to select my flow follow script in the script folder.
Or we can simply select the camera parent in which game object we just created the follow script.
So and you you can see in our camera parent.
So I'm going to open that up by double clicking on it.
So we're moving the camera over here.
So before we move we're going to check if the game is active or not.
Means our player game object is alive or not.
If it's not then we need to stop moving our camera.
So we're going to check before we move.
We're going to check.
If so we will go to Game Manager script.
Then we're going to go to instant.
And then we're going to access the is game active parameter.
And we will check if this equals equals to false.
If that is the case means our player game object totally get destroyed.
So we're going to make some curly brackets.
So in that situation what we need to do we need to stop moving our main camera or the camera parent
to the right direction.
So we're checking over here.
So we're just going to game manager then instant and then accessing the parameter which is game active.
And then we're checking if it's equals equals to false.
Basically we're just going over to this Gamemanager script and we accessing this game active parameter.
And we're just checking if this equals equals to false.
And we do know where we're setting.
This is game active parameter to false.
When our player game object as as you can see at that moment we're just setting it equal to false means.
Our player game object is totally.
So in that situation what we need to do, we need to stop moving our main camera.
So the main camera or camera plane.
So that's why we're going to type this return.
And we do know after return whatever we're gonna do it's not going to be executed.
Means our main camera will stop moving to the right direction.
So firstly we're just going.
Are we basically checking is game active parameter is false or not.
If it's then we're just typing.
Return means after return whatever we're going to do it's not going to be executed.
So this line of code we are not going to execute means our camera parent or the main camera will stop
moving to the right direction.
So basically that is the thing.
And now we're going to save our script by hitting Ctrl s.
But before I do I'm going to make this one as cool.
And I'm going to put this curly brackets over here.
And I'm going to press Ctrl s to save my script.
And let's move to unity.
So you can use that Game Manager script to check everywhere whatever you're doing.
Like moving your crown, moving your main camera, like all of those things.
So just wait for it to compile.
And now it's fine if I select Game Manager and let's click on this play button in the top and have a
look on this.
So as soon as the player game object is gonna die, you will see this camera just gonna stop moving
to the right direction or this camera parent game object.
So let's have a look on this.
As soon as this player game object is gonna die, you will see that.
So it's taking damages.
And we can see now, as soon as our player game object dies, we can see the camera just gets stops
moving to the right direction.
And before it dies, I also want to show the explosion effect, as we have done with this enemy two
as well as enemy one game object.
So we're gonna spawn our explosion prefab or the effect before we die.
So let's get out of play mode and let's open up the player skip where we just want to show.
So once our player game object dies.
So I'm going to select my player game object in the hierarchy.
And let's open up the player skip by double clicking on it.
So we want to show up the explosion before we die.
So before we die, we're going to use this instantiate function in order to spawn.
So as a first argument it's going to take the game object or the explosion effect that you want to spawn.
And the second thing it's going to take the position.
And the third thing it's going to take the rotation.
So let's make this one a comment by now for double forward slash.
And let's go to top of our class.
And we're going to make over here something over here.
Let's do that.
So we're going to make a serialized field that will be private.
We're going to set.
So even we set it to private we're still going to able to see this parameter underneath our player skip.
Why.
Because we marked as a serialized field as you can see.
So if you little bit confused then you can just go over to my Clean Code chapter and then uh, and then
serialized field, uh, lecture.
So you can go and check that out how you can make your clean go, uh, how you can make your code clean
so you can check that out about serialized field as well as Hide in Inspector and many things.
So this will be game object that in explosion effect itself a whole game object.
So that's why we need to type as a game object.
And I'm going to call this one to something like explosion effect prefab or so.
Simply we can type explosion effect as well.
And now we're going to press down Ctrl S to save our script.
And let's move to unity.
So underneath our player Skip we're going to able to see in the bottom uh right before this maxhealth
parameter a field will appear and we just name it Explosion Effect prefab.
So what we have to right there.
What we have to do right there.
We basically need to drag the explosion effect GameObject into that slot.
So I'm going to move over to my project tab, then assets and then prefabs.
And I can see I have the explosion effect over here.
So we can what we can simply do.
But before we do I'm going to check uh, this folders as well.
So I don't have.
So I'm going to select my Explosion effect prefab game object and let's drag it over to that slot explosion
effect prefab slot underneath our player skip.
And now we can see we have the explosion effect game object and we are ready to spawn that.
So let's go to Skip and let's move over to die function where we're doing.
And I'm going to remove this slashes as the first thing.
It's going to take the game object you want to show or the explosion effect.
So I'm going to pass this explosion effect prefab.
And we do know we just assign it.
So make sure you just go to unity and drag the reference into that slot.
And the second thing it's going to take the position.
So I'm going to pass transform dot position means the center position or the current position of our
player game object.
And the third thing it's going to take the rotation.
So I'm going to pass quaternion.
Dot identity basically means no rotation.
And let's close that up with semicolon.
And we're going to press down Ctrl S to save our script.
And let's move to unity.
So we will see.
Uh, as soon as this player came up, it's just gonna die.
The explosion effect will be spawned at our player position.
So let's have a look on that.
So I'm gonna press down, uh, click on this play button at the top to play our game.
And let's have a look on this.
So as soon as this player game object just gonna destroy, we will able to see that explosion effect
prefab right away.
So just wait for to compile.
So now we should able to see, as you can see, as soon as it's died we can see the explosion effect.
But the problem is that, uh, if we move over to hierarchy, we can see still see the explosion clone
or the explosion effect still laying around the hierarchy as well as in the scene view, as you can
see.
So after some period of time, we also need to destroy this explosion effect prefab as well.
And we also need to adjust the explosion effect where we want to spawn.
So we're going to create an empty game object for that.
So let's get out of play mode and let's uh select the player game object.
Let's right click on it and let's create an empty game object.
And I'm going to press down F in order to focus on that.
And let's select the move tool in this in view.
And I'm going to move it somewhere over here.
Uh so this basically I want to spawn from this, uh, position of that explosion effect.
So that's why I just created an empty game object.
And I'm going to call this a game object to something like explosion, uh, effect point or spawn point,
explosion spawn, uh, position, or let's call it point instead.
And I'm going to choose an icon for that.
And this seems pretty much fine.
So I'm gonna spawn it from here because, uh, as we seen this, the explosion just if spawning a more
deeply or in the foot of our, uh, player game object.
So I'm just gonna move it a little bit up, or I just want to show up a little bit from this player.
Feet.
So that's it's the reason.
And let's open up the player script.
But before we do, let's double click on this explosion, uh, effect prefab game object in our prefab
folder.
And after doing that let's go to animation tab.
And we need to check the animation so we can see after 0.9 second.
So this over here is basically means one second.
So after 0.9 second it's just completing the whole animation clip as you can see.
So after this period of time we need to uh, basically we need to destroy that, uh, this explosion
effect game object as well.
We need to destroy after this amount of time because it's just completing the whole explosion effect
animation after this period of time.
So that's why.
So let's go back and let's go to script.
So the first thing we need our position from where we want to spawn.
Instead of spawning center position of player, we're gonna spawn that from that position as we created
in in our hierarchy inside our player game object.
So let's go to the top of our class and let's grab the reference, uh, basically the position from
where we want to spawn.
So let's create a serialized field that will be private and that will be transformed.
Basically means position.
And we can simply call it something like as explosion, explosion effect, spawn position or spawn point.
Let's call it.
And that's it I'm gonna call it explosion spawn point, something like that.
And let's go to that script where we spawning.
So over here.
So I'm going to instead of passing transform.position basically means center position or the current
position of our player game of a game object.
I'm going to pass explosion if spawn point dot position.
So we're going to spawn the explosion effect from this position.
And we have the uh, we need to grab the reference first.
And after spawning what we're going to do, we're going to store this explosion explosion effect that
we spawn it, we're going to store it into another game object or a, another container, you can say,
and we're going to call it temp effect, something like that.
So we just created a game object called temp effect.
Basically we just created a game object container because this explosion effect prefab itself a whole
game object.
So that's why we need to put it a container or in a container.
Or you can simply say in a same variable or the same parameter name.
So we created this temp effect and that is a game object type container.
So firstly we spawn it at this position with no rotation.
And after that we're just taking that we spawn it which is explosion effect.
And then we're just storing into this stem effect after spawning that.
So we also need to destroy this container or where we store it or where uh in which uh parameter or
variable we store it.
We also need to destroy that as well after some period of time.
So we're going to use the destroy function.
So let's use this destroy function.
So it's going to take as a first argument that game object you want to destroy.
So in our case stem effect or where we store that explosion effect after spawning.
So in our case this stem effect we store it.
So we also destroying this stem effect.
And the second thing we're going to specify the time.
So we do know that 0.9 second it's just completing the whole animation clip of explosion effect.
So that's why as a second argument we're going to pass 0.9 F.
And let's close that up with semicolon.
And we're going to press down Ctrl S to save our script.
And let's move over to unity by now.
So you will see that explosion effect prefab will also be destroyed after 0.9 second after completing
the whole animation clip of that.
So just wait for it to compile and once it's done, we can simply select Player Game Object and let's
go to overrides and then click on Apply All and we can see in our player skip a explosion effect prefab
we have but the explosion spawn point we need to drag it and it says none transform basically empty.
So we're going to select our underneath underneath our player game object which is explosion spawn point.
And we're going to drag it into that slot.
Now we have the explosion spawn point basically the position from where we want to spawn it.
And now we're going to click on this play button in the top to play our game.
And let's take a look on this.
So just wait.
And we will see that sorry we jump it a lot and we can see as soon as we die we can see the explosion
effect.
And if we move to hierarchy, we are not able to see that explosion effect prefab as well as in the
scene view means after that amount of time, it just gets destroyed right away because we spawn it first
and then we just taking that and we store it into this effect and we also destroying the temp effect
after this amount of time where we store it, as you can see in our code.
So I hope you get the idea.
And we seen that the explosion effect is just spawning from feet of our player game object.
So you can just adjust the position as well.
If you just want to spawn it in a specific position, then you can create an GameObject and then you
can just simply, uh, use that position.
So I'm going to get out of play mode.
So basically that is the thing I hope you get the idea of this how you can spawn it.