Creating a Start Menu in Unity 5

Sharing is caring:

What’s the first thing players see in your game? The start menu!

In this Unity tutorial we’ll take a look at creating a simple start menu in Unity 4.6 and above. This start menu will contain two buttons, one to play our first level and one to exit our game. If we click on exit we will then be prompted with a pop up menu asking us to confirm if we want to quit our game, if so then we’ll quit the game.

 

In this tutorial you’ll learn how to:

  • Create a screen space overlay Canvas
  • Use Text and Images for menus
  • Apply Button functionality to text elements
  • Use C# to create functions for playing a level and exiting the game
  • Setup basic levels within the build settings
  • Enable and disable sub-menus

  Creating menus within a game is very important. A start menu is the very first thing people see when they want to play your game and having a simple menu system that takes players from one scene to the next is a must. This tutorial will teach you the basics of creating a simple start menu with the added bonus of creating a sub-menu to prevent players from accidentally exiting your game. The sub-menu will display a pop up that asks if the player wants to leave the game, if so then they can. If not, they can click on no and return back to the default menu.

Font Used: http://www.dafont.com/zombie-control.font

 

Menu Script (C# File)

using UnityEngine;
using UnityEngine.UI;// we need this namespace in order to access UI elements within our script
using System.Collections;
using UnityEngine.SceneManagement; // neded in order to load scenes

public class menuScript : MonoBehaviour 
{
	public Canvas quitMenu;
	public Button startText;
	public Button exitText;

	void Start ()

	{
		quitMenu = quitMenu.GetComponent<Canvas>();
		startText = startText.GetComponent<Button> ();
		exitText = exitText.GetComponent<Button> ();
		quitMenu.enabled = false;

	}

	public void ExitPress() //this function will be used on our Exit button

	{
		quitMenu.enabled = true; //enable the Quit menu when we click the Exit button
		startText.enabled = false; //then disable the Play and Exit buttons so they cannot be clicked
		exitText.enabled = false;

	}

	public void NoPress() //this function will be used for our "NO" button in our Quit Menu

	{
		quitMenu.enabled = false; //we'll disable the quit menu, meaning it won't be visible anymore
		startText.enabled = true; //enable the Play and Exit buttons again so they can be clicked
		exitText.enabled = true;

	}

	public void StartLevel () //this function will be used on our Play button

	{
		SceneManager.LoadScene (1); //this will load our first level from our build settings. "1" is the second scene in our game

	}

	public void ExitGame () //This function will be used on our "Yes" button in our Quit menu

	{
	Application.Quit(); //this will quit our game. Note this will only work after building the game

	}

}

Jonathan Gonzalez

I love all things in game development and want to contribute by teaching others how to create games. I'm always looking to help others create awesome digital worlds. I've been a hardcore gamer since I was a kid and now it's my turn to create awesome digital experiences.

More Posts - Website

Follow Me:
TwitterYouTube

Comments

  1. I want to learn says:

    Hi
    I got two problems when I followed your tutorial. First one was about the graphics and the second was about the script.
    I will ask you first about the graphics problem.
    After I created the title of the game and the other buttons and placed them correctly in unity editor, I build the scene to stand alone and found out that the tile and the other buttons position is changing according to the screen resolution I am trying the build on. It has no auto scale that keeps the position. At one screen resolution It appeared on the far left, in another resolution appeared near the middle of the screen.
    How to fix this?

  2. I want to learn says says:

    Thanks a lot for the answer and I hope you make a video about it soon. Learning from a video is way better than text.

    • I’m mostly creating video content for Cgcookie.com at the moment and revamping all their Unity fundamental courses. I will be including a UI course that covers all the basics of using different UI elements including how to build for multiple platforms.

  3. I want to learn says:

    Can you please give me a quick explanation about how to make an auto scale splash screen? I have an image with size 1920×1080 that I want it to be my splash screen but I am having all kinds of troubles to make it works on all screen resolutions.Please help me out here.

    • The link I provided can explain things a lot better than I can. If you’re using a 1920×1080 sprite (image) then change the width and height of the image UI to that size (not the scale). Then apply your sprite to it. After that you’ll want to click on the canvas itself and find the “Canvas scaler” component. On there you’ll need to change the drop down from “Constant Pixel Size” to “Scale with Screen size”. Then set the reference resolution to 1920×1080. Everything else will be default. Now in the game window if you change resolutions you’ll notice the image will scale relatively well with the different sizes while trying to match the same resolution.

  4. Neat Tutorial says:

    Thanks for this! I followed the tutorial and it goes at a great pace. I’m basically going through all your Unity-introduction youtube videos. Super great.

    For beginners, would you also be able to maybe make a tutorial on making custom buttons (eg special shape/background with text)? Or novice, making a toggle menu that affects the transparency of a gameObject?

    • Creating custom buttons is more of a Photoshop or design type of thing. I did go over how to create buttons in a very old tutorial here: https://www.youtube.com/watch?v=H4Tpaq6D5cI

      Button toggles are not something I’ve used yet so I can’t really tell you when that might happen. I’ve actually been creating content for CgCookie.com so I haven’t been able to really do any other video content. I am making quite a few Unity 5 basic courses which will be released in the near future (on CGCookie.com).

      • Neat Tutorial says:

        Thank you for the prompt response and also the older tutorial, making .png wasn’t really the issue I was having: working in Unity 5 I’m unable to create GUI texture that’s described in that tutorial. I can add a texture onto the empty gameObject itself, but no luck trying to add it to actual buttons (I can’t drag it onto Source Image or Target Graphic).
        Thanks again for your help.

        • GUI Textures aren’t really ideal. Using the UI system is far more intuitive and easier to use. I can’t really give any guidance on that since I haven’t used GUI Textures since the new UI system has been implemented. Also just to be clear, the GUI system Unity had (like GUI Texture) does not work in the same way as the UI system. So trying to add UI buttons onto the GUITexture wouldn’t work as they work completely differently. Using the old GUI system requires a lot of manual scripting hence why it was a paint to use. If you want to follow along with the section of that tutorial that covers buttons using Gui Textures you can find that here: https://www.youtube.com/watch?v=-iRBr-Y7GGw

  5. Hanan Elhariri says:

    I’m try to create High score button, but i can’t
    Can you help me if you can

  6. Hanan Elhariri says:

    Create start menu game (Play,High Score, setting, Exit)
    i can’t code the (High Score)

  7. Plz help me bro.
    When I tried add the script to my StartMenu it says this:::
    http://postimg.org/image/hp9s2hbbb/
    Plz, help me. I am workng on a game. PLZZZZZZZZZZZ…………

    • The script name and the class name need to match. So if the script is called “menuScript” then the class also needs to be called “menuScript” otherwise it won’t recognize it.

      • Alexis Dumas says:

        Where can i find the “class” and how can i know his name?

        • If you open up the C# script typically the third line down is the class name. It’ll say something like “public class menuScript : Monobehaviour” and typically when you first create a script in Unity it’ll name your class the exact same thing. It’s important to note that if you change the file name but not the class name Unity will think that script doesn’t exist because the class and script name don’t match exactly. You’ll know this is the issue when you try to add a script with conflicting names and it say something like “Cannot add script because it does not exist”.

  8. WOOOW IS AMAZING THANKS FOR INFO 🙂

  9. I love your tutorial I followed it step by step, I want to thank you for making it, it worked flawlessly but a day later it is giving me a an error, I may have messed it up some how but I cant figure it out.

    UnassignedReferenceException: The variable quitMenu of menuScript has not been assigned.
    You probably need to assign the quitMenu variable of the menuScript script in the inspector.
    UnityEngine.Component.GetComponent[Canvas] ()
    menuScript.Start () (at Assets/Scripts/menuScript.cs:14)

    • this actually doesn’t harm any thing its just when I Move my buttons using the rect transform, and like fro example i put the button to the left it breaks everything, and it just doesn’t work after ward. and Im using unity 5 personal

    • That pops up when you haven’t assigned a variable. Typically this will happen if you move things around as that reference is now “broken” so you’ll need to assign things to the variables again. It won’t break your game unless you need that variable for your script to work but either way it’s a quick fix.

      • Great tutorial, but I moved some things around and am getting the “UnassignedReferenceException” message as well. How do I go assign things to the variables again?

  10. Nomadic says:

    Another great video, is there second part of this video how to save and load game data?

  11. Hello how do i add an options sub menu? in the menu scene ? anyone can help me?

    • The same way we created the “quit menu”. Just create another sub menu using an image/buttons then use a script to enable/disable it when you press the “options” button on the main menu.

  12. QueenKx says:

    Everytime I add the script to the start menu it never comes up with ” Quit Menu ” , ” Start Text ” , or ” Exit Text ” . Could you help me? Im using Unity 5.1 also..

    http://imgur.com/6WGWEdr

    • Are you getting errors in the console? Make sure your script name and class name match exactly otherwise you’ll get errors.

      • QueenKx says:

        its working now, thank you.

      • I had this problem also and it was because there was an error in the console (I added a scene in the application load that doesn’t exist yet).

        Once I made it a comment, it worked. Just for those who want to know.

        Also using Unity 5.1.

        • If the scene doesn’t exist then it doesn’t know what to load. Making it a comment doesn’t fix it, it just essentially removes that part of the script since it thinks it’s a comment instead. You’ll need to create the scene and add it to your build settings (file>build settings).

  13. Can you make a tutorial on how to combine the new unity 5 multiplayer networking with the menu?
    I have a working multiplayer game with the new networking system build in unity 5 but when i use your menu, offcourse Application.LoadLevel (1); overrides all of that so i need a bit of help….. thxx

    • I’m not very familiar with multiplayer in Unity. I would assume you’d load a specific scene to play online, just replace the “LoadLevel(1)” with your scene. The “1” just refers to the first scene within the build settings, this is meant to be customized to what you want.

  14. Hi! Your video is great… however my Unity states that the script cannot be loaded and is not valid, but there aren’t any compiler errors or anything like that. Just that it won’t load…

  15. Hi, thanks for your great tutorial!!!
    It works fine, but only in Unity! If I build my game and it and I would click on one my two menu buttons. The game chrasehed. There are no errors or something like this. I have no idea. I hope you can help me! Thanks.

  16. Jared Smudde says:

    I keep getting this error: http://pastebin.com/XN4ThARG

    My menuScript.cs code is: http://pastebin.com/K8zHtHa6

    What is going on? I set up everything the same way you did in the video. Please help!

  17. here in canvas only 3 texts with only one line.what if there are texts each containing 5 lines…i mean i have descrption UI text consisting of 3 lines , Hint UI text with 7 lines , Rule UI text with 5 lines , start UI text with one line , exit UI text with one line , welcome UI text .the whole content should fit in canvas.but because of too much content i am unable to show it in the canvas.usually on the start menu , description , hint , rules will be displayed …even i am trying the same….but i am unable to do it…..guide me pls…..

  18. i have set both horizontal and vertical to overflow in the text component….but even then whole content doesn’t fit in the canvas…suggest me how to fit content in the canvas

  19. hi jonathan,
    could u tell me how add file upload control ( in which we can upload files) pls

  20. hi jonathan
    i have follwed this starting menu tutorial……i have build and run it ……yes button functionality is not working i mean i am unable to quit the application , but if i click on yes the quit menu disappears….could u tell me pls why is it happenig

  21. Assets/menuScript.cs(5,14): error CS0101: The namespace `global::’ already contains a definition for `menuScript’
    Help me

    • If you double click the error in the console window it’ll take you to the part of your script that is having an issue. From what the message says, I believe you have “menuScript” applied a few times. If you copied the script make sure you overwrote everything within the script and didn’t paste it inside another classed called “menuScript”.

  22. please translate to javascript these strings

    public Canvas quitMenu;
    public Button startText;
    public Button exitText;

  23. Your tutorial was phenomenal. Thank you! Will be citing you in my credits for the game I am making.

  24. Hi, I followed your tutorial and it works. Great! But I have one problem, how can I make the canvas disappear when I hit the “play” Button, this really annoys me. Thanks!

  25. 26 error found in script

  26. I just want to say a great thank you for this video and c# script! This is so useful and everything is just absolutely clear!

  27. you are awesome bro. your tutorial helps me a lot to understand how the code works.

  28. Chandramauli Kaushik says:

    When I press the NO button, The quit menu does not disappear but the play and quit buttons are enabled. What could be the reason for that? I copied the code from above.
    Please Help

    • Are you getting any errors? It just sounds like something is not assigned properly and/or a button is referencing the wrong function. Double check that the quit menu was assigned to the correct slot and you’re using the correct OnClick events for the buttons.

  29. Everything works except when it loads the scene the scene is dark, I loose the lighting. If I load the scene directly the lighting is fine.

    Any ideas Please?

    • That’s probably a bug with Unity. I’ve experienced something like that before where loading a scene from the editor resulted in a dark scene, but opening it up separately didn’t show up dark. You can either bake the lighting in that scene or just build the game and test it. It only seems to happen when switching scenes in the editor.

  30. Hi, first of all i have to say that your tutorial is great.I followed up your instructions but i have a single problem.
    Everything is working perfect on inspector(buttons,scripts.load other scene) but when i build and run the project nothing is working from the menu.I mean that my scripts dont work.I checked class names ,script names and i cant find what is wrong.Note that the other seen i have is working when i build an run the project
    Can you please help me?

    • Does this work when you’re in play mode? I wouldn’t build the game until you know it works in play mode. Not sure what else to mention since I don’t have a lot of info to go on. I would make sure that your OnClick events are using the correct methods to call when clicked. Also make sure you have your scenes in your build settings otherwise it won’t know which scene to load. I’ll need more specific info on what works and what doesn’t work in order to help you out more.

      • Yes it works perfect when im on play mode.Button,scripts everything.My Menu scene is absolutely the same with that in your video .It is a ver basic menu with start and exit button start jumps to next scene and exit button goes to yes or no,but none of them are clickable when i build it !also OnClick is working when im on play mode so something doesnt work when i build it.After build and run(both of my scenes) i get only a frozen picture of my menu.
        Really apreciate your response!

        • Honestly sounds like something is hanging up. Usually when something like that happens to me it’s because a scene is a bit too resource intensive for my computer to run properly and/or it’s about to crash. I would recommend loading a very generic scene(s) (blank scene works too) with the menu to see if it’s the loading scene that’s causing the hang up. Other than that I would just test various parts of the UI bit by bit. Seems a bit weird that it would work in the editor but not in a build.

  31. I made a new scene,copy paste everything and its working.I really have no idea what was the problem but its ok
    Thank you very much for your responses.Keep up your great work!!

  32. Thanks this tutorial is flawless and work very well. thanks again for all the hard work. but while in game how can i make a pause menu that takes me back to the main menu?

    • Hello Victor, to create a pause menu you would create a script that would detect a specific key input. For example you could have it setup so that when someone presses “P” it pauses the game and displays the pause menu. Something like this:

      public GameObject pauseMenu; //your pause menu group that contains buttons/images for what they’ll see when paused
      private bool isPaused; //this will be a bool that is toggled on and off
      void Update(){

      if(Input.GetKeyDown(KeyCode.P))
      {
      isPaused = !isPaused; //this toggles the bool on and off every time “P” is pressed
      PauseMenuGroup(isPaused);
      }
      }

      void PauseMenuGroup(bool showMenu)
      {

      if(showMenu){
      Time.timeScale = 0; //this stops time in the game creating the “paused” effect, may not apply to everything
      pauseMenu.SetActive(true); //displays menu
      }

      else{
      Time.timeScale = 1; //this re-enables time in the game
      pauseMenu.SetActive(false);
      }

      }

      To return back to the main menu you’d have a button similar to what we had in the tutorial for starting a new level. In this case you’d just have it load the first level (the main menu scene). Remember to use the SceneManager namespace: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

Speak Your Mind

*