We're going to create a script in order to basically if we click on this retry button, then we want
to reload the level.
And if we click on this menu button, then we just want to move over to our panel scene or menu.
And we don't have the menu yet, so we're going to create it later.
For now we're going to print out a message.
But before we do, let's create the script in order to load our level.
To do so, let's head over to our project App assets and then script folder.
Let's right click there create and then move over to scripting.
And then click on this Monobehaviour script.
Or you can simply click on this Monobehaviour script in the top, as you can notice.
And you can call that script to something like scene management or something like Game Manager as well.
I already created a script called Scene Management, as you can notice right over here.
So we need to attach this script and to any GameObject in our hierarchy to make it work.
Or we can simply create another GameObject for this script separate game object.
So let's do that for it.
So I'm going to move over to hierarchy.
Let's right click or hit this plus icon.
Let's create an empty game object.
And let's call this game object to something like scene management.
So I'm going to call this one scene management.
And let's go over to Inspector Transform.
Let's right click on it and then reset.
And I'm going to select my scene Management game object.
And I'm going to drag it in the top of my hierarchy so I can see all the time.
And we're going to select this scene management script.
And let's drag it over to this Add Component section in Inspector.
And let's open up our scene management script by double clicking on it.
And let's remove this update and start function.
We don't need uh, so whenever we working with scene, make sure you just go to top of your class and
include this namespace.
And it's called using Unity engine Unity engine dot scene management.
So make sure you include this namespace.
Uh just move over to top of your class and include this namespace called using Unity Engine dot Scene
Management, because we're gonna work with scene.
So that's why.
And we need to make this function public since we're going to call this function through a button.
So if I move over to unity and then go over to canvas, then UI or this BG basically, and then select
this retry button or this menu button, then go over to Inspector.
Scroll down.
We have a button component.
And in the bottom we can see on click.
So we can call a function over here in order to do something.
So that's why we need to make those function public so we can just access it over here.
So whenever we're going to click on this retry function add a retry button.
Then that function just gonna trigger.
So that's why.
So let's move over to our scape.
So we're going to create public.
And for function we're going to type void.
And we have to name the function.
So I'm going to call this function to something like retry retry level, something like that.
And let's make first bracket and some curly brackets.
So inside this function what we want to do we just want to reload the scene which we are currently in
or which level we are currently in.
So we are currently in this uh higher.
If we move over to hierarchy, we can see the name of the scene is Sample Scene.
And if you want to see the build index of it then what you can do you can go to top left in unit editor
says file.
Then click on this build profiles in the bottom and you can see the name of the scene.
So right over here it's going to be Scene List.
And then click on this open scene list.
And you can see the name of the scene and the build index.
We can notice in the right section which is zero.
So we can simply pass the name of the scene or the build index of it.
So I'm going to cut this step nine right now.
And let's move over to our script.
And in order to load the level we need to use this scene manager.
Scene manager dot load scene function.
So make sure you use this function in order to load the scene or the level that you want to load up.
So scene manager dot load scene.
And inside this uh first bracket, we can pass the name of the scene in quotation or the index of it
without quotation.
But we're gonna use something else.
So we're gonna use this scene manager as scene manager.
Dot get active scene.
Then we're going to make this first bracket, and then we're gonna use this dot, and we're going to
pass the index of it and then close that output semicolon.
So what basically this means scene manager dot get active scene.
The active scene basically means the scene which we are currently in.
So we are if we move over to unity, we can see we uh, we currently in our, uh, sample scene and
then what we're doing, we just accessing the index of it.
And if we move over to unity, then top left file, then build profiles so we can see the name of the
scene is sample scene.
in and in the right section we can see the build index is zero.
So over here it's just going to be zero right away.
Why.
Because scenemanager dot get feet.
SIM basically means the scene which we are currently in or which level we are currently in.
And then we accessing the build index of it.
And we saw the build index is zero.
The scene which we are currently in.
And we do know currently in basically the scene is sample scene and the build index of it is zero.
So basically means this scene manager dot load scene function is going to load the scene which we are
currently in.
Basically it's going to load up the samples in itself.
So I hope you get the idea.
And now we're going to press down Ctrl s on our keyboard in order to save our script.
And then we're going to move over to unity.
And we need to hook this retry button with that retry retry level function.
So just wait a moment.
And once our compilation is done, we can just go ahead and we can hook this retry button with our retry
level function.
Now the, uh, now, uh, the script gets compiled.
So now what we have to do, we need to move over to hierarchy, then canvas game over ui bg.
And then let's select this retry button.
Let's go over to Inspector.
Scroll down to the button component and we can call the function over here on click.
So we need to click on this plus icon.
And we need to drag the game object in which game object the script is sitting on.
So if I select my Scene Management GameObject in the hierarchy and then go over to Inspector, we can
notice Scene Management Script is sitting on this scene Management game object.
So that's why we need to select this scene management game object.
And we need to drag it over here and it says no function.
So just click there and we will go over to scene management script or Scene Management class.
And in there we can see we created the retry level function.
So we're going to choose that.
It It's basically means as soon as we're going to click on this retry function, then that function
is just going to call out which retry.
So basically we're just moving over to this scene management class or this script.
And then we just call it we just triggering this function.
Basically whenever we're going to click on that retry button then this function just gonna get called.
And what this function is really doing, it's just loading the scene which we are currently in.
As you can notice.
Now we're gonna move over to unity.
And let's click on this play button in the top.
And let's have a look on this.
So as soon as we're going to click on that retry button then retry level a function just going to call
out.
And we do know what we're doing inside that retry level function.
So just wait a moment and we're going to take damage from this.
You can simply say this show or this fire.
So as you can see our retry basically this BG just pops up.
And if I just go ahead and then click on this retry button.
As you can notice, as soon as we click we just loaded the scene which we are currently in.
So we are currently in sample scene and we just accessing the build index of it.
And we do know the build index is zero.
So basically just it just reloading itself.
And let me show you one more time.
So let me show you.
And now we can just click on it retry button in order to reload the level.
And if we just go ahead and then click on this menu button.
This button is doing nothing currently and we don't have the menu scene yet.
So for now we're gonna print out a message if this button is working perfectly or not.
So we're gonna check that out.
And if we click on this retry button then we just reloading the level.
So we're gonna get our get out of play mode.
And now let's create another function for this menu button.
So let's move over to our scene management class or script.
And let's make another function for that menu button.
So we're going to make public void for function.
And we're going to call this a load menu.
Or let's simply call it menu.
Let's make first bracket and some curly brackets.
So we don't have the menus in yet.
So if this button is working or not we're going to print out a message.
So I'm going to type this Debug.log for printing out message.
And we're going to say something like uh load menu.
So let's say something like load menu and load menu.
And let's close that up with semicolon.
Sorry.
We're going to close that off with a semicolon.
And we're going to press down Ctrl S to save our script.
And now let's move over to unity.
So we need to hook that menu function with our menu button.
So as soon as we're going to click on this menu button that function just gonna trigger.
And what that function is really doing is just printing out the message in the console says load menu.
As you can notice inside this function.
So we're going to select our console tab in order to see the message.
So just wait a little moment.
So now if I select my console tab.
But before we do, make sure you select your menu button underneath your game object, underneath your
Game Over UI.
Then go over to Inspector and make sure you select the menu button.
Then go over to Inspector.
Scroll down to the button component and then click on this plus icon.
And we do know that we need to drag the game object in which game object the.
This basically script is sitting on.
So in our case scene management.
So we're going to select that and let's drag it over to that field.
And it says no function.
So let's click there and let's go to scene management script or scene management class.
And we can see the menu function right over here.
So we're going to choose this menu function for this menu button.
So as soon as we're going to click on this menu button that menu function just gonna trigger.
And what this menu function is doing.
It's basically printing out the message in the console says load menu as you can notice.
So now let's move over to unity and let's select the console tab.
And let's click on this play button in the job in order to play our game.
And let's take a look.
So as soon as we're going to click on that menu button, that menu function just gonna trigger and that
menu function basically just loading up the message.
Sorry.
Printing out the message says load menu.
So let's take damage from this or this wraps first.
And we can notice it gets pop up.
So now if I just go ahead and then click on this menu button.
As you can see as soon as we're clicking this menu button, if we move over to our console tab, we
can see a message just getting pop up says menu.
So we basically are printing out this message to check if this button is working or not.
Because we don't have the menus in yet.
We need to create.
So that's why for now we just printing out the message.
So as soon as we're clicking on this menu button, that menu function just calling and what that menu
function is doing, it's basically the printing out the message load menu in our console tab, As you
can notice.
So I hope you get the idea that how you can do it and we're gonna get out of play mode and make sure
you select the parent game object.
So in our case this game over UI and go over to Inspector then overrides and then click on this apply
all.
So the changes you've made inside this game over UI game object it's going to be save.
And let's also make this scene management game object a prefab.
Why?
Because we need this scene management script.
So that's why.
And the scene management script is sitting on the scene management game object.
So that's why we're gonna make this one prefab.
So we're gonna have the copy so we can use this scene management script to any scene that we want.
So I'm going to select my project tab and let's select the Scene Management GameObject in the hierarchy.
And let's drag it in inside this prefabs folder in order to turn it into a prefab.
So as you can notice, we just turn it into a prefab.
Basically, we have the copy of it by now, and we can use it into another level or another scene as
well.