Unity – Create a simple main menu

  1. First of all, create a new Scene by going to your Scenes folder under Assets and right click Create Scene. Name it something like MainMenu. While you’re at it, rename your main scene to something better if it’s named SampleScene to MainScene.
  2. Create a new GameObject under the UI category and choose Button.
  3. Go to the Text child object under the Button and Choose Text, change the Text from to something like Start Game like the picture below.
Main Menu in Unity
Start game button in Unity

4. Select the Button and Add Component, and select New Script, name it btnStart.

5. Double click on the btnStart script name to open up Visual Studio to start editing it, and add the following.

using UnityEngine;
using UnityEngine.UI;

public class btnStart : MonoBehaviour
{
    Button _btn;
    // Start is called before the first frame update
    void Start()
    {
        _btn = this.GetComponent<Button>();
        _btn.onClick.AddListener(TaskOnClick);
    }

    void TaskOnClick()
    {
        Debug.Log("You have clicked the button!");
    }
}

As you can see in the Console in Unity, we are now getting a log message when we click the button. We can now simple do whatever we want, such as go to our MainScene.

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class btnStart : MonoBehaviour
{
    Button _btn;
    // Start is called before the first frame update
    void Start()
    {
        _btn = this.GetComponent<Button>();
        _btn.onClick.AddListener(TaskOnClick);
    }

    void TaskOnClick()
    {
        SceneManager.LoadScene("MainScene", LoadSceneMode.Single);
    }
}

If you get the error:

Scene ‘MainScene’ couldn’t be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
To add a scene to the build settings use the menu File->Build Settings…
UnityEngine.SceneManagement.SceneManager:LoadScene (string,UnityEngine.SceneManagement.LoadSceneMode)
btnStart:TaskOnClick () (at Assets/Scripts/btnStart.cs:17)
UnityEngine.EventSystems.EventSystem:Update ()

Then go to File in the top left, and select Build Settings… and click on Add Open Scenes for your MainMenu and MainScene.

That is the essentials of it! Now you can change up colors and make things more interesting of course. I thought about adding more to this tutorial but don’t think it’s necessary since it is kind of straight forward.

0 0 votes
Article rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments