Simple Main menu

Niklas Bergstrand
3 min readApr 8, 2021

Games normally do not jump straight into the action when first turned on, but more often lands on a menu. Let’s add a Main menu to the space shooter and go over how to load scenes.

Firstly, create a new scene by opening the File menu and selecting New Scene:

Just after the new scene has opened, make sure you save the scene straight away so that the scene file gets created:

We can use the same background as in the main scene. Drag in the background and set the Clear Flags on the camera as well as background colour to black. Resize the background sprite to fit the whole screen:

Right-click on the hierarchy and add a UI image:

Set the source image, click on “Set Native Size” and anchor and position the image (remember to hold alt to automatically position the UI object):

Click on the Canvas and set the UI Scale Mode to ‘Scale with Screens Size’ to ensure the size is the same on all devices:

Add a button to the hierarchy:

Create a new script called MainMenu and attach to the Canvas:

Open Build Settings from the File menu and add the two scenes - MainMenu and MainScene. Ensure that MainMenu is on index 0 and MainScene is on index 1:

Open up the MainMenu script and add the namespace UnityEngine.SceneManagement and a public function called StartGame. Without the namespace you would not be able to access the class SceneManager. Add SceneManager.LoadScene to the function and add 1 as the index to load the MainScene:

Rename the button object and set the text inside the button to “START GAME”:

Add an OnClick event to the button and drag in the the Canvas. Set the function to call to StartGame:

Final result:

Good luck!

--

--