Let's say that active this game over UI through our code.
So we're going to set that.
So let's select our player GameObject in the hierarchy.
And let's open up the player script quickly by double clicking on it.
So what we basically need if we move over to unity underneath our canvas game object, we can see in
the hierarchy we have game over UI.
So we want this to be active.
But when we shoot, once our player GameObject loses all of his health, then we're gonna active this
game over here by.
Basically, if we select our game over UI GameObject underneath our canvas, then move over to Inspector,
we can check.
Basically checking means we just activating that.
And if we just uncheck, we mean we basically means we just deactivating that.
So we need a reference of this game over UI GameObject, basically this whole game object.
Then we can just set that to be activate.
Or we just want to enable or disable depending upon if the player game object is alive or not.
So let's select our player game object in the hierarchy, and we're gonna open up our player skip by
double clicking on it.
And let's go to top of our class.
Uh, over here in the bottom I'm going to do uh, so let's make a serialized file.
So let's make a serialized field.
Serialized field, private private.
And that came over by game object itself, a whole game object.
So we're going to type game object, uh, the game object that we're looking for, basically that game
over game object itself, a whole game object.
So that's why we need to type the game object, uh, type.
And we can simply call this one something like game over UI.
And let's close that up.
Semicolon.
And I'm gonna press down Ctrl s to save our script.
And let's move over to unity.
So underneath our player we're gonna see another field just gonna appear.
And by by default, we just named it Game Over UI.
So just wait.
So as soon as our player skip gates compile underneath our player skip in the bottom, we can see Game
Over UI and it says none game object.
So basically what we have to do over here we need to select our game over UI game object and drag it
into that slot from the hierarchy.
Now we can see we have the game over UI, game object prefab or the game object, and we are ready to
activate or enable that.
So let's open up our player.
Skip quickly one more time, and we're going to scroll down to the bottom where we dying.
So over here as you can see we're calling the type function and die function.
Just doing the rest thing like setting is game active to false showing up explosion effect, all of
these things.
So we also want to activate that game over UI as well.
So what we will do, we will go to Game Over UI and we're going to use this set active function.
And this set active function in brackets.
It's going to take the value.
So if you just pass right over here true.
So it's basically means that's gonna be activate right away means we are if we select our game over
UI game object and check this in Inspector.
So set active true basically means we're just activating.
And if you just pass false means we just unchecking or deactivating our game over UI.
But we do want we just want to activate.
So that's why in these brackets I'm going to pass the true value.
So we will pass true value.
And let's close that up with semicolon.
So I'm going to pass through.
Let's close that up with semicolon.
So basically what we're doing we're just going over to this game over UI game object in the hierarchy.
We can see.
And then we just activating or setting active to true means I just checking you can simply say and now
we're going to press down Ctrl S to save our skin.
Now let's move over to unity.
So you will see, uh, as soon as our player GameObject loses all of his health, then we're calling
the die function.
And inside that die function, we just activating that game over UI and make sure you just drag the
reference in your player.
Skip, uh, have just take a look in Inspector, and we are ready to play our game.
So let's click on this play button in the top to play our game.
And let's have a look on this.
So as soon as this player GameObject Conor loses all of his health, you will see this, uh, the game
over.
Just gonna activate right away.
So just wait for it to, uh, take damage from this enemy GameObject.
And we're going to take this, uh, take, uh, enemy uh, sorry damage from this enemy game object.
So let's see.
And we can see as soon as our player GameObject object as this game over us gets activate and if we
click on this retry button, we can see this button doing nothing currently.
So we need to hook this button with some certain function.
So if I select my retry button.
So if I move to hierarchy then game over UI game object underneath my canvas.
And then select this retry button.
Then move over to Inspector.
Scroll down.
We can see a button component is added to this button.
This retry button.
So over here what we're gonna do if we scroll down a bit we can see on click and it says list is empty.
Basically we can just call a function over here as well.
So as soon as we're gonna click this retry button and we're going to call a function over here.
And inside that function we're going to do the thing that we really need to do.
Uh, like uh, like loading up the levels in one more time so we can call the function on click basically
underneath our button over here in the inspector.
As you can see.
So we can call a function.
So let's get out of play mode.
And we're going to create another script in order to uh, in order to click on that retry button.
Basically as soon as we're gonna, uh, basically gonna click on that retry button, we just want to,
uh, load up the level one more time.
So we're going to create a script for that.
So make sure you just go to Project App then assets and head over to Script Folder.
Let's right click there then create and then Monobehaviour script and name that script to something
like Scene Management.
Don't name it to something like Scene Manager.
Instead name it to something else.
You can name it to scene management or you can say scene, uh, load or some something like that.
So I already have created a scene manager.
Screen scene manager.
Uh, sorry.
I just name it to scene manager.
It should not be scene manager.
And instead we're gonna create a scene management because it's gonna to give us an error.
So that's why.
So we're going to right click on this script folder one more time.
Let's click go to create in the top.
And then Monobehaviour script.
And I'm going to name this script to something like Scene management not scene manager.
Don't name it to scene manager, name it to something else.
So I'm going to call it scene Management.
And let's hit enter.
And we're going to double click this script to open it up in Visual Studio.
And we need to attach this script to our game object.
So what I'm going to do I'm going to create a separate, uh, game object for this scene management
script.
And I'm going to attach this script to that game object.
So just wait for to compile and run.
Once you are done, we are ready to do that.
So let's go to hierarchy.
Right click or hit this plus icon.
Let's create an empty game object.
And I'm going to name this game object to as the name of our script.
So I'm going to call it Scene Management.
And let's go to transform.
Right click and reset the transform and I'm going to select my scene management script.
And let's drag it over to this Add Component section.
And I'm going to drag my Scene Management GameObject in the top of my hierarchy so I can see all the
time.
And let's open up the scene management script by double clicking on it.
And I'm gonna remove this update and start function we don't need.
And we're going to work with scene.
So make sure you, you just go to top of your class and include this namespace it call using Unity Engine,
Unity engine, dot scene management, scene management.
And then close that up with semicolon.
So what we have to include this namespace in top of our class because we're gonna work with scenes or
levels.
So that's why we need to include this namespace in top of your class.
So make sure you just do that.
And we are ready to create our functions.
So we're going to create two functions.
One for just gonna just gonna be for this retry button.
And the second button.
Uh second one gonna for this retry button.
And the second function we're gonna make for this menu button.
So let's go to Skip now and make sure you make this function public.
Why?
Because we're going to call this function through our button.
Basically whenever we're going to click on this button I just want to call this function.
So that's why.
So we're going to mark as a public function.
And for function we're going to type void.
And I'm going to name this function as something like retry button for my retry function.
So I'm going to call it retry.
Instead.
Let's make first bracket and some curly brackets.
And I'm going to put it for line space or from the left side.
So what we want to do inside this function we just want to load up the scene.
So in order to load up a scene or the level what you have to do, you need to include this function.
It's called scene manager dot load scene function, load scene function.
And we're not getting.
So I'm gonna what I'm going to do, I'm going to move over to my unity because I already created a scene
manager script, as you can see.
So we need to delete that.
So that's why it just not happening.
So make sure you just select the scene manager and just delete.
I don't name that.
Uh, basically name a script something like scene management.
Don't name it something like scene manager.
So that's why.
Or you can simply call level manager as well that script.
So I have already created a scene manager script.
And I'm going to delete that because I created a second script called Scene Management as you can see.
So I'm going to select my scene manager script and I'm going to delete that because it just come are
conflicting the error.
So just right click that and then click on delete in the top you can see.
So delete that.
Now it should be fine.
So make the script name scene management.
Don't name it scene manager.
And you can name it something else.
And that is fine.
Just don't name it something like Scene Manager because we need to use that function.
Now it's done.
If we move over to script Scrape, and in order to load up a scene, we need to use the scene manager
dot load scene function.
Now we can say and this function in brackets is going to take the same name in quotation or a.
You need to pass the bill index of it.
So how you can see the name of the scene and the built in build index of it.
So move over to unity.
In the hierarchy top you can see the name of the scene.
We can see example scene, but we're not able to see the build index of it uh of the scene.
So what you really have to do, you need, you need to move over to this top left file.
So just click there and then click on this build profiles.
In the bottom you can see and uh we and as soon as we uh, we're going to click on this open scene list.
And now we can see the name of the scene is sample scene.
And the build index.
We can see in the right section which is zero.
So you can uh, what you basically have to do, you will be windows or something like that.
So just click on this open scene list and you can see the name of the scene as well as the index of
it.
So the name of the scene we can see in the left side, which is sample scene, and the index of it in
the right section which is zero.
So I'm going to cut this tab by now and let's move over to script.
So in quotation what we simply do, what we simply can do.
We can simply pass name of the scene which is sample.
So sample scene.
And let's close that up with semicolon or what you can simply do over here.
You can simply pass the bill index without quotation.
Uh so the index is zero.
But I'm going to pass the name of the scene in my in quotation in between this quotation, which is
sample scene.
So I'm going to pass my sample scene and let's close that up with semicolon.
And let's move over to unity.
So this scene manager dot load scene function it's gonna load up the scene.
And in brackets it's going to take the name of the scene.
So we also need to call this retry button.
Uh sorry retry function and retry function.
Just going to do these things which are just going to load up the scene.
So let's move over to unity.
And I'm going to select my retry button underneath my game Over UI game object.
Let's go to Inspector.
Scroll down and scroll down to this button component.
And in the on click and it says list is empty.
So just click there and over here what you have to do.
You need to drag the game object in which game object the script is attached.
So in my case I attach the scene management script to this scene management game object.
So I'm going to select that GameObject and drag it over to this slot.
And it says no function as you can see.
So just click there and we're going to move over to scene Management script or scene Management class
as you can see.
And we're going to call the retry function.
So basically what we're doing we're just going over to this scene management and it says no function.
So just click there.
And we do no in that scene management game object the scene management script is attached.
So that's why we are able to see the scene management class over here or the scene management screen.
And uh, and we can see retry function as well as we created inside scene management script or scene
management class.
And we just made that retry function public.
So that's why we are able to see over here.
So I'm going to choose that function.
So now if we just click on this retry button, as soon as we click on this retry button you will see
that retry function just going to call.
And what we what we have done in this retry function we just loading up the scene in which scene we
are currently in.
And let's go to unity now.
And let's click on this play button in the top in order to play the game.
So just wait for to compile and you're gonna see that.
So as soon as we're gonna lose health, uh, a health app, basically after taking damage, you will
see this player game object just gonna die right away.
And after that, that came on, came over.
You are just going to be activate.
Activate.
And we have two buttons or two option to do in in that game over.
So just wait.
And now if I just click on this retry button, as you can see, as soon as we click on that retry button,
we just loading up the scene in which scene we are currently in.
One more time we're going to get out of play mode.
And instead of doing that, you can do that thing as well.
So let's go to script instead of passing the name of the scene in quotation or the bill index of it,
you can use these things as well.
It's called scene manager, scene manager dot get active scene and make first packet.
And then you have to use this build index.
So what basically this means scene manager dot get active scene basically means the scene in which scene
we are in.
So if we move over to unity we can see in hierarchy we are in this sample scene.
So the scene manager Dot get active scene right over here.
Just going to be the sample scene.
And then what we're doing, we're just accessing the build index.
And we know the index of this of this sample scene is zero.
So basically we're just loading up the scene in which scene we are currently in.
So scene manager Dot get active scene basically means the scene we are currently in.
So we are currently in this sample scene.
So then what we're doing we just accessing the index of it means we just loading up the samples in one
more time.
So this scene manager Dot get active scene means the scene which we are currently in.
And then we just accessing the index of it and we're just loading loading that up I'm really apologize.
Uh, that bus just making noises I'm really apologize for it.
So we're just loading up the scene, which scene we are currently in.
So you can just pass this as well once you just retrying.
So let's go to unity and let's have a look.
So I'm going to press down Ctrl s on my keyboard to save my script.
And let's go to unity.
So once we're clicking on that retry button then what we're doing we're just moving over to this scene
management game object and then scene management script.
And then we're just calling the retry function once we click on that retry button.
So let's go to unity one more time.
And if I just click on this play button in the top, you will see as soon as we're going to click on
that retry button, the the scene which we are currently in, it's going to load up one more time because
we're calling that function.
So just uh, let's wait to take damage from that enemy game object and you will see that.
So just wait, uh, to take damage from this enemy game object.
And now we're just taking damage from this enemy game object.
And this seems fine.
And now we can see game over UI just gets activate.
And now if I just click on this retry button, that retry function just gonna call.
And we do know what we have done inside that retry function.
So if I just click on this, as you can see, as soon as we click the scene that we are currently in,
we just loaded up that one more time.
And you can see the name of the scene in hierarchy.
Over here we can see the name of the scene is sample scene and the build index of it zero.
So I'm going to get out of play mode.
And now what we have to do if I just activate this game over UI.
Now we need to work with this menu button.
So once we click on this menu button I'm going to call the I'm gonna load up the menu scene.
But we don't have the menu scene yet.
So I just want to print out a message if it's working fine or not.
So let's go to Scene Manager manuscript and I'm going to make a second function in the bottom.
So let's make that function as well public.
So we can call through a button.
And for function we're going to type void.
And we have to name the function.
So I'm going to call it panel something like that.
And let's make some curly brackets.
And, uh, so we don't have the menu scene yet so we can load that up.
So I'm going to print out a message instead.
So printing out a message we're going to type Debug.log in order to print out message and make first
bracket.
And in quotation we're going to say something like load menu load menu.
And let's close that up with semicolon.
And we're going to press down Ctrl S to save our script.
And I'm going to move this curly bracket over here.
And let's press down one more time Ctrl s to save our script.
And I'm going to hook this menu button or this sorry this menu function with our menu button.
So as soon as we're going to click on that menu button we're going to call that menu function.
And what menu function is doing is just printing out a message in the console says Load menu.
So just wait.
And once it's done we are ready to do that.
So now we're going to select our menu button underneath our Game Over UI game object.
So let's select the menu button.
Let's go to Inspector.
Scroll down to the bottom button component and we can see on click and it says list is empty.
So let's click there and we're going to drag the game object in which game object the script is sitting
on or attached.
So in my case scene management.
So let's drag right there and let's click on this no function.
Let's go to scene management see class or Skip.
And we can see the menu function over here because we made it public.
And we're going to choose this menu function.
Because once whenever we're going to click on this menu button we just want to call up call that menu
function.
So.
So as soon as we click on this menu button you will see menu function just going to trigger or just
going to call.
And inside that menu function we just printing out a message in the console says load menu.
As you can see.
And make sure you just go to unity.
And by default I'm gonna deactivate this game over UI.
I don't want this to be activate.
So let's uncheck in the inspector and let's click on this play button in the top to play our game.
And let's have a look.
So whenever as soon as we're going to click on that menu button you will see the message in the console.
Just gonna pop up says Load Menu.
So I'm going to select my console tab in the bottom.
And let's take damage from that enemy game object.
So just wait a little moment.
And we're going to take damage from this enemy game object.
So we're taking damage as you can see.
And now the max is zero.
So this game over UI just gets activated.
And if I just go to click on this menu button, as you can see as soon as we click menu button, uh
or basically click menu button, that menu function, just get call.
And inside that menu function we are just printing out a message in the console we can see load menu.
So as soon as we're going to click on this menu button, that menu function just calling.
And we do know what we have done inside that menu function.
We just printing out this message a load menu in the console as you can see.
So I hope you get the idea that how you can call a function through a button.
So basically you can call the function through a button by going over to button component.
And you need to drag the game object in which game object the script is attached or sitting on.
Then you can just call the function from that script in which script you have done all of these things.
So I'm going to get out of play mode.
I hope you get the idea that how you can do it, or how you can just print up, and we also need to
create the scene as well in order to load that up.
So I'm going to press down control S in my you can say keyboard to save my project.
And I'm going to move over to my Game Over UI game object.
Let's go to overrides.
And I'm going to click on this Apply all.
Now it's fine.
And I'm going to press down one more time Ctrl S to save my project.