How to create simple menu navigation and switch between menu screens in Unity

I’ve made a step by step video tutorial to create a basic menu and navigation script in Unity3D.

So basically you can see the Main Menu with two buttons to navigate to its sub menus, and in each sub menus you have a back button to go back to Main Menu.

In this video you will see how to switch between menu screens in one single Unity scene.

Watch the video and for coding part, create a C# script in Unity then copy and paste this code into the file:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MenuScript : MonoBehaviour
{

    public GameObject MainMenu;
    public GameObject Screen1;
    public GameObject Screen2;

    // Start is called before the first frame update
    void Start()
    {
        BackToMainMenu();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void HideAllScreens()
    {
        MainMenu.SetActive(false);
        Screen1.SetActive(false);
        Screen2.SetActive(false);
    }

    public void OpenScreen1()
    {
        HideAllScreens();
        Screen1.SetActive(true);
    }

    public void OpenScreen2()
    {
        HideAllScreens();
        Screen1.SetActive(true);
    }

    public void BackToMainMenu()
    {
        HideAllScreens();
        MainMenu.SetActive(true);
    }
}

loading...

Leave a Reply

Your email address will not be published. Required fields are marked *