Loading Scenes in Unity

Day 24- Making 2D Space Shooter Game

Loading a new Scene in Unity can be very straightforward, requiring only a single line of code.

Here, we want to allow user to restart the game when player presses a ‘R’ key after the Game over screen is displayed.

In UIManager, you are going to need to add a namespace SceneManagement which holds the library of all the methods for scene.

First off, go to File<Build Settings and select Add open scenne which will add your current scene in the build. Also Note the Build Index of your scene which is zero on the right side of your scene.

Now, if you create a condition in update which is important since you want to check if the key is pressed every frame and key to be pressed is ‘R’ then load the scene with Build index ‘0’.

And now you get yourself a restart but be sure to add a text to tell player which button to press to restart the game. Because of _isGameOver bool variable we designed in the last article, the ‘R’ key will now work only when the game over screen appears i.e. player is dead.

Unity Developer