Legacy GUI pause menu issue

I’m trying to write a pause menu with the Unity Legacy GUI (IMGUI).

Currently the menu opens when I press ESC and closes with either the ESC key or the Resume button in the menu, I’m trying to make it so when the "Settings" button is pressed it switches to a Settings menu with different button.

Currently it renders both the "Pause Menu" buttons and the "Settings Menu" buttons on top of each other. I don’t understand since it SHOULD only render the Settings menu when settingsMenu boolean is true, which is set to false in the Start method.

Here is the code:

void OnGUI()
{
    if (isPaused == true)
    {
        //enable cursor etc
        Cursor.lockState = CursorLockMode.None; Cursor.visible = true;
        playerController.movement_IsLocked = true;
        cameraRotation.camera_IsLocked = true;

        pauseMenu = true;
        GUI.skin = skin;
        GUI.Label(new Rect(0, 0, 1920, 1080), "Paused");
        if (pauseMenu == true)
        {
            //Menu UI
            GUI.Box(new Rect(100, 100, 200, 60), "" + gameInfo.gameName, skin.customStyles[3]);
            if (GUI.Button(new Rect(125, 200, 150, 50), "Resume")) isPaused = !isPaused;
            if (GUI.Button(new Rect(125, 250, 150, 50), "Settings")) pauseMenu = false; settingsMenu = true; ;
        }
        if(settingsMenu == true)
        {
            //Settings UI
            GUI.Box(new Rect(100, 100, 200, 60), "" + gameInfo.gameName, skin.customStyles[3]);
            if (GUI.Button(new Rect(125, 200, 150, 50), "Options")) ;
            if (GUI.Button(new Rect(125, 250, 150, 50), "Controls")) ;
            if (GUI.Button(new Rect(125, 300, 150, 50), "Video")) ;
            if (GUI.Button(new Rect(125, 350, 150, 50), "Audio")) ;
            if (GUI.Button(new Rect(125, 400, 150, 50), "Back")) settingsMenu = false; pauseMenu = true; ;
        }

    }
    else 
    {
        //modify cursor and unlock controls
        Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; 
        playerController.movement_IsLocked = false; 
        cameraRotation.camera_IsLocked = false; 
    }
}

Well, if your settings menu is shown, that means that your settingsMenu boolean is actually true. Note that your pauseMenu is actually ALWAYS drawn since you set your pauseMenu boolean to true right before your if statements. So that “if” could actually be removed in it’s current form because you can never “not” make the pause menu shown whenever “isPaused” is true.

Note that you used GUI methods which requires you to specify a Rect for each element. When you use GUILayout instead, you don’t have to specify the Rects of each element as they are calculated each frame in the layout step of the OnGUI callback. You can use GUILayout.BeginArea with a Rect to define a new layout section and the layout elements would automatically arange in that area. Be default the normal layout direction is vertical. But you can create and nest BeginHorizontal and BeginVertical sections. Just always make sure you end a BeginXXX call with the corresponding EndXXX call. Also note that the GUILayout methods have an optional params array at the end which allows you to pass additional parameters to control the element. So you can set just the height of elements, change the min / max behaviour and many other things.

You may want to have a look at my IMGUI crash course post I made some time ago. I originally was a post on Unity Answers but the formatting here got messed up so I copied it to github so it’s safe from Unity messing with this site ^^