We're going to clean up our code.
So let's do that.
Firstly let's go with this player script.
So I'm going to select my player GameObject in the hierarchy.
And let's open up the player script by double clicking on it.
So instead of using find any object by type we're going to use a singleton pattern.
So basically what we're going to do we're going to go to the top of our class.
And over here we're going to create a variable.
So we're going to make it public.
So we can easily access through any other class or any other script.
And we're going to create a static.
Static basically means that we can simply type the this player class name.
And then we can access any variable that we would like to access.
We can access this dumb height.
We can access this speed through another script or another C sharp class that by directly typing this
player script name and that that is the reason we just using this static.
And we're going to create a variable as the name of our this player script or this player class.
So we're going gonna create a variable, uh, and as the name of our, uh, C class, which is player.
So we're going to create a player and we can call simply this one something like instance.
So we're going to call it instance uh instance.
And we're going to close that up with semicolon.
And we will use this awake function.
So we're going to use this private void awake.
So make sure you just include as I did over here basically this function get call right before this
start function.
And it's basically calls as like a start function.
But it get call right before this start function.
And it just get call one time.
So in this start of the game what we want to check, we want to check if the instance is single or not.
If basically we just want a one player strip in our sample scene or in this game.
So we only want one player skip.
And we don't want to have multiple player skip.
So it's gonna be error.
So that's why.
So we're going to create an instance.
Instant.
So we just created this public static player instant and we will move over to this awake function.
So in the awake function we're going to set instant equal to this.
So we just storing all of the information.
Basically this update function this update function.
Then this should function of this player class into this instance parameter.
As you can see we just set instant equal to this.
So we're storing all of the information about this player class into this instant parameter.
And by now we can extract information by going over to this player script.
And then instant.
And we can extract anything that we would like to extract.
Something like we can extract this health text as well from another class.
If we want this rigid body and anything that uh, anything like that, this start function as well,
as well as any custom function that we have in this player, skip, we can easily access.
But before we set instant equal to this.
We need to check if the script is a one time or not.
Basically, we only want to have one player script in this whole game or in this level one, you can
simply say so we only want one script.
So before we set instant equal to this, we're going to check.
If so, we will check if instant instant is equals equals to null if that is the case.
So we're going to make some curly brackets.
So if instant equal to minus the null then we're going to basically we're going to cut this one which
is instant equal to this.
And we're going to paste inside of this curly brackets.
So we're checking if instance equals equals to null.
Basically means this player script.
Uh this player script is empty.
Basically we don't have any player skip or this player game object.
You can simply say or win in which game object the script is attached.
So in our case the player skip attached to this player game object.
So we're checking if instance equals equals to null.
Mean we don't have any player script.
Then what we're doing we're just setting instance equal to this.
So in that situation we're just storing all of the information about this C-sharp player class into
this instance parameter.
And we can extract info by going over to this instance instance parameter instead of using that which
is fairly taxing, like find or any object by type.
And then you have to pass the script name.
So we can simply just go move over to this player script and then instance.
And we can extract any function or any variable that we're looking for directly.
So that's why we just created public static player instance.
And let's suppose we already have a call player script.
So in that situation what we need to do we need to delete or we need to destroy this instance.
Then we don't want.
So we're going to else else we already have a instance.
Then we're going to destroy the instant or this player class that we're storing information.
We're going to destroy it right away.
So we're going to use this destroy function.
In order to do that.
And in this brackets you need to pass the game object.
So we're going to pass itself.
So with the script this player's script is attached to our player game object.
So the player game object just referencing itself.
So we're just destroying.
And we're going to close that up with semicolon by now.
And then we're going to press down Ctrl S in order to save our script.
So we're just storing basically we just created a variable as the name of our player skip as you can
see.
So and we're storing all of the information about this player class or this player script into this
instance parameter.
As you can see if the instance is equals equals to null basically means empty.
Then we're just setting instance equal to this.
Basically storing all all of the things in this in this instance parameter or this instance parable.
Now we're going to move over to player sorry unity.
And we're going to access the player script or anything that we extracting from this player game object.
So basically this enemy game object we do know it's going to keep track of where player is.
And we're just going over to this player script.
And then we just extracting information about this player game object.
So what we're going to do over here, we're going to move.
We're gonna open up the enemy tool script as well as enemy one script in order to do that.
So we're going to select our enemy to GameObject in the hierarchy.
And let's open up the enemy two script quickly by double clicking on it.
And over here as you can see we're checking if call info then game object dot get component.
And the component we're looking for is not equal to null.
Then what we're doing we're just going over to that player game object.
Basically this player game object.
And then we just going over to this player script of this player game object.
And there we're just calling the function, which is the take damage.
And we do know what we have done inside that function.
So instead of using this long term like this, we're going to simply go to that player skip.
So what we're going to do we're going to go over to that player skip.
So we can simply pass the script name.
So in our case player.
And then we're going to use this dot and we're going to access the instance.
And we do know we just created the instance parameter right there as you seen in our player skip or
that player C class.
And then from there by this instance parameter.
Now we're going to call the function.
And the function we're looking for is a take damage.
So we're going to type this take damage.
And we're going to make first bracket.
And in argument I'm going to pass one.
And let's close that up with semicolon.
So as you can see this seems too much clean by now.
But instead of using this call info that dot GameObject, that dot get component, which is fairly taxing.
So basically we're just going over to we just simply pass the player, script name.
So in our case player as you can see, and then going over to that instance and then calling the Takedamage
function, basically we just going over to this player script and then we're just going over to this
instance parameter.
And by this instance we're just calling this Takedamage function basically as we have done before.
So we're just doing the same thing.
But uh, by but uh, right now we're just using this instance parameter.
So the uh creating singleton pattern makes our code a lot more clean.
And if you haven't grabbed it, uh, perfectly, or if you just want to get it in more details, then
of course I will also gonna give a another video link in the resources so you can check that out about
a singleton pattern if you just want to learn it more.
So we basically storing all of the information about this player class into this instance parameter.
And then we're using this instance parameter in order to call the function, and we use this static.
So we can simply pass the player screen name.
So as you can see we have done over here, which is we just simply pass the player script name.
Then we just moving over to instant.
So make sure you just using that static.
Else you're not gonna able to do like this.
So so you can simply pass the script name without hesitating like that doing anything.
So that's why we just created a static parameter which allow us to use shortly.
Basically, we can just simply type the script name which the, the script that you looking for.
And then you can just do that as I have done over here.
So I'm going to press down Ctrl S in order to save my script.
And now let's move over to unity.
And we're going to do the same thing in my enemy one game object as well.
So just wait for to compile.
And now if I select my enemy one game object I'm going to do the same thing in this anime script, this
anime one script as well.
So I'm gonna open up my anime script by double clicking on it and let's go to the top of our class so
we don't need to do that.
Uh, let's create let's create that.
But before we do, I'm gonna do it, uh, basically over here, as you can see, we're doing basically
we damaging our player over here.
So we're gonna move over to that player class, and then we're going to access the instant.
And by this instant I'm going to call the Takedamage function.
And let's make first bracket.
And then we're going to close that up with semicolon.
And also we need to pass the argument.
So basically we're just going over to this player script and then going over to this instant.
And by using this instant we also call it the Takedamage function in that enemy one script as well.
So I'm going to move over to anime script one more time, and let's press down Ctrl S in order to save
the script.
And now let's move over to unity.
So you can approach the same thing with this enemy one game object, as well as with this two game object
as well, so you can simply create singleton pattern in this enemy two script as well as the enemy one
script.
So you're gonna have that.
So before we do, let's have a look by now.
So I'm going to click on this play button.
There should be no errors in our console tab.
So before we do let's have a look on this.
And we're gonna do the same thing in this enemy two game object.
Basically enemy two script as well as enemy one script.
So we're not able to see any error in the console.
So you can just ignore that error, which is not at all thing.
And we can see the player game object indeed taking damage.
And once it loses all of his health, we can see a message in the console tab.
Just kidding.
Pop up says player died because we do know once our player game object loses all of his health, then
we're just calling the type function and we do know what type function really doing.
So I'm going to get out of play mode by now.
I hope you get the idea that how you can do it, and I'm going to do the same thing in this enemy anime
to skip as well.
So I'm going to open up my anime script and let's go to top of our class and let's make it public.
So make sure you making it public so you can just access through another class or another script easily.
And we're going to create a static parameter and we're going to call it anime two.
Basically we're just creating a variable as the name of our script or this C class, and we're going
to call it instant as we have done.
And we're going to set.
So let's use the awake function.
Or you can use the start function as well.
It's totally up to you.
So in this script or this anime script I'm going to use the start function.
And we do know start function, get call and the start of the game.
And in the start of the game I'm going to check if instant is equals.
Equals to null means empty.
Then we're going to set instant equal to this.
So we're going to set instant equal to this.
Else we're going to destroy our instant.
So we're going to use this else condition.
And we're going to use this destroy function in order to destroy.
So we're going to destroy this.
And let's close that up with semicolon.
And let's do the same thing in our anime one script.
So I'm going to move over to my anime script or the first anime and let's create a another public static.
And this will be enemy.
And I'm going to call this one instant.
And let's go to start function.
Uh, for this script I'm going to use the awake function.
So let's use this awake and this awake function get call uh, in the start of the game right before
this start function.
And it also get one time just one time.
And we're gonna check if instance equals equals to null.
Then we're going to set instant equal to this instant equal to this.
So let's set that.
And if in already there is an instant then we're going to destroy this instant this.
And that's it.
And I'm going to move over to my player script where we destroying or just damaging our player.
So I'm going to move over to my player game object.
So over here, as you can see we just are damaging our player and we're just using fairly taxing or
heavy texting, heavy text in order to do that or in order to damage our enemy.
So instead of using this, we can simply go to enemy to script, or we can simply pass the script name.
So in our case this enemy to a.
So we're going to go to instant.
And now we're going to call that takedamage function.
And let's make first bracket and then close that up with semicolon.
And once I damaging enemy one then I'm going to use this enemy one script enemy.
Then we will move over to instant.
And we're going to call that Takedamage function.
And over here it's going to take the argument.
And I'm going to pass one.
So it will just going to subtract by one unit.
And I'm going to press down control S in order to save script.
And let's do the same thing with this ground spawner as well.
So it's going to be fine.
So I'm going to move over to.
My over here.
Let's go to top of our class and let's make a public static.
So let's make a public static that will be ground spawner.
And I'm going to call this one instant as well.
And for this script or this class I'm going to use this start function.
And I'm going to check if instance x equals equals to null.
Then we're going to set instant equal to this.
So instant equal to this.
Else we're going to destroy that.
So let's use this else condition.
Else.
If already there is an instant then we're going to destroy this instant.
So we will destroy this.
And let's close that up with semicolon.
And I'm going to move this curly brackets a little bit up.
And let's move over to player script where basically we're just calling the ground spawner function
in order to spawn our ground.
So let's go there.
And over here, as you can see we are we're using this find any object by type.
And then we're just going over to ground spawner script and then calling the spawn ground function,
which is fairly taxing as you can see.
So instead of doing this, we can simply, uh, now we can simply pass our ground spawner as class name.
And then we're going to move over to instant.
And now we're going to call the spawn ground function.
And let's make first bracket.
And let's close that up with semicolon.
And then we're going to press down Ctrl S to save our script.
And let's move to unity by now.
So just wait for to compile.
And once it's done we are ready to do that basically.
Basically we just calling those function.
So just wait.
And now it should be fine.
And you can ignore this error.
It's not much at all effective.
So we can simply clear that.
And let's click on this play button in the top.
And let's have a look on this.
If it's perfectly working or not.
So we're going to take a look on this.
So we're using singleton pattern in order to do that.
And if you still a little bit worried about a singleton pattern.
And if you just didn't understand it well uh, I will have a video in the resources, a link, you can
go and check that out about, uh, singleton pattern that we use in this, uh, our project.
So you can take a look on that.
I hope you get the idea that how you can use singleton pattern in order to make your code clean, and
that's it.
Uh, that that's pretty fine.
And also, we need to adjust our background perfectly in order to seamlessly.
It's gonna, uh, see we're gonna see that it just, uh, spawning, uh, top of our, this background.
So it should, uh, so we're going to, uh, take a look on this, uh, thing in the end of the, our
lecture.
So I'm going to get out of play mode.
I hope you get the idea.
And I'm going to press down Ctrl s in order to save my project.