Unity Scene Management
10K views
Oct 30, 2023
Unity Scene Management
View Video Transcript
0:00
Hello guys this is Vivek Sharma and welcome to yet another Unity tutorial on C Sharp Content.com
0:05
In this video we will be learning about scene management and we will be creating a restart screen for our game
0:11
So without wasting any further time let's get started. So to begin with let's go to our pause menu and create a new button to restart our game
0:24
To do that I'm just going to copy the resume button. I'm going to change the font. I'm going to rename the button and I will remove this function
0:31
Now let's go to our scripts folder and create a new script and rename is add load scene
0:36
Let's double click on it to open this up. First of all, we will be deleting the start and update method from here
0:42
And then we will have to use another namespace called unityengine.scenemanagement. Using scene management, we can load different scenes
0:50
Now let's create a new public function called load screen, which takes a string as a parameter
0:55
which is going to be the name of the scene you want to load. Inside this, we just have to write scene manager dot load scene
1:01
and it takes the name of the scene you want to load as a parameter. Now let's get back to Unity
1:07
Go to Canvas. Click on add component and let's add the load scene script to our Canvas
1:12
Now click on our restart button. Go to the inspector panel and on click event
1:17
Let's add the function we just created. That is load scene script load scene
1:23
Perfect. Now over here we have an exposed string field in which we have to enter the name of the
1:28
scene we want to load. Let's write game scene into it. Let's go back to assets panel
1:34
Click on the scenes folder and rename the sample scene to the game scene
1:39
Now let's hit play, pause the game and click on restart button to restart the scene and
1:44
also get ready for a new problem So over here as you can see our dinosaur is not moving The reason being when we paused our game we set the time equal to 0 and when we restarted it we haven changed the time scale to 1 again
1:59
So to do that let's go back to our load scene script and over here let's write time.timescale
2:05
equal to 1 and it should work now. Let's get back to unity
2:09
Let's hit play and as we can see everything is working properly
2:13
Now let's quickly create a restart screen as well which shows up whenever the game ends
2:17
To do that go to canvas, create an empty game object, call it game over menu, inside it create
2:23
an image which we will be using as a background, now create a text and just write whatever
2:27
you want to write, game over UDIT whatever you wish, I will be using UDIT and then quickly
2:33
create a new button, change its size, change its font and rename it to restart button and
2:39
Then as we done earlier, assign the loadScene function to this button
2:43
Now let's go to our dino script. Let's create a public game object and call it gameOverScreen
2:50
Now go to the killDino function and just write gameOverScreen.setActive equal to true
2:56
That's it. Let's get back to Unity now. Click on the Dino Player. Go to the dino script
3:01
Over here we have got an exposed field for our gameOverScreen. Drag the gameOver menu from hierarchy panel to this exposed field
3:09
let's hit play. So now as you can see whenever our dino dies we get this restart screen and by
3:15
clicking on restart button we can restart the game. Okay so that's it for this video and probably
3:21
that's it for this game development series as well. I hope you guys liked this series and also
3:25
have learned something. If you guys have any doubts related to this series or related to anything
3:30
regarding unity you can get in touch with me anytime. So goodbye, have fun and don't forget
3:36
to follow and subscribe because the best is here to come