C# Start Menu building

Hello guys/girls,

Finally found out how to write a start menu script with the build in gui system.
However I’m running into a problem.
When I click the options button my options menu appears.
But when I then click on credits the options menu is still there with the overlaying credits menu.

How can I fix this?

using UnityEngine;
using System.Collections;

public class Layouttest : MonoBehaviour
{
		public bool showOptions = false;
		public bool showCredits = false;
		private int guiButtonWidth = 100;

		void OnGUI ()
		{

				GUILayout.BeginArea (new Rect (0, 0, 200, 300));
				GUILayout.BeginVertical ();

				if (GUILayout.Button ("Start Game")) {
						Application.LoadLevel (4);
				}
				if (GUILayout.Button ("Options")) {
						showOptions = true;
				}
				if (GUILayout.Button ("Credits")) {
						showCredits = true;
				}
				if (GUILayout.Button ("Quit Game")) {
						Application.Quit ();
				}

				GUILayout.EndVertical ();
				GUILayout.EndArea ();	

				if (showOptions) {
						showOptions = true;

						GUILayout.BeginArea (new Rect (210, 0, 200, 300));
						GUILayout.BeginHorizontal ();
						//text column
						GUILayout.Label ("Quality settings", GUILayout.Width (100));
						GUILayout.EndHorizontal ();
						//settings column
						GUILayout.BeginHorizontal ();
						GUILayout.Button ("Fastest");
						GUILayout.Button ("Fast");
						GUILayout.Button ("Simple");
						GUILayout.Button ("Good");
						GUILayout.Button ("Beautiful");
						GUILayout.Button ("Fantastic");
						GUILayout.EndHorizontal ();
						GUILayout.EndArea ();
				} else {
						showOptions = false;
				}
				if (showCredits) {
						showCredits = true;
						GUILayout.BeginArea (new Rect (210, 0, 200, 300));
						GUILayout.BeginHorizontal ();
						GUILayout.Button ("You did it");
						GUILayout.EndHorizontal ();
						GUILayout.EndArea ();
				} else {
						showCredits = false;
				}
		}
}

Can’t this be achieved simply by adding a single line:

if(showCredits) {
     showOptions = false; 
     .... 

}

So how would I write that for both menu’s?

if (showCredits) {
						showOptions = false;
				}
				if (showOptions) {
						showCredits = false;
				}

doesn’t work.

Try changing your if statements to:

if(showOptions && !showCredits){
...
}

…and visa versa. Yes/no/maybe?

if (GUILayout.Button (“Options”)) {
showOptions = true;
showCredits = false;
}
if (GUILayout.Button (“Credits”)) {
showCredits = true;
showOptions = false;
}

also you have some redundant code…

                 if (showOptions) {
                 showOptions = true;

you are saying “if showOptions is true, then make showOptions true” also the else{} statements are doing the same thing.

remove the showOptions = true; and the else{} lines, or you can leave them in if you like, they just don’t do anything.

In OnGUI() the last line of code executed will appear on top. Instead of using [if else] use if for checking the condition

 if (GUI.Button ("Options"))
{
     Resetvalue();
     showOptions = true;
}
if (GUI.Button ("Credits"))
{
    Resetvalue();
    showCredits = true;
}    

if (showOptions) 
{ 
  ///// Options menu Script.  
}  

if (showCredits) 
{  
 ///// credits menu Script.  
}       


void Resetvalue()
{
  showOptions = false;
  showCredits = false;
}