Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Issue with using ESC to open and close a button/window

Discussion in 'Immediate Mode GUI (IMGUI)' started by Graveyard-Gamers, Apr 20, 2014.

  1. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    Ok I have been working on this for a few hours now. I have played around with my code and got the GUI window and the Yes button to not show while I play game. Is this the best code setup for what I am trying to do? Will the window and button always be there taking up cpu with this code set up?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExitGame : MonoBehaviour {
    5.  
    6.      // this is the window for the exit yes button
    7.  
    8.     public bool ExitGameWindow = false;
    9.  
    10.     // This is the Exit Yes button
    11.  
    12.     public bool ButtonYes = false;
    13.  
    14.  
    15.  
    16.  
    17.    
    18.     // Use this for initialization
    19.     void Start () {
    20.  
    21.         Screen.showCursor = false;
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.  
    28.         // This will show the mouse cursor when you hit Escape key and open the ExitGameWindow.
    29.  
    30.         if (Input.GetKey (KeyCode.Escape)) {
    31.             ExitGameWindow = true;
    32.             ButtonYes = true;
    33.             Screen.showCursor = true;
    34.                 }
    35.     }
    36.  
    37.  
    38.  
    39.     // Void OnGUI() is the function for your Graphic user Interface (GUI)
    40.     void OnGUI() {
    41.        
    42.         // Make a backgroud for the button.
    43.         // This type of code will never allow the box with the buttons in it to change postion on screen no matter what screen size.
    44.         // As of now the if(ExitGameWindow) makes it so when you start game this window does not show.
    45.  
    46.  
    47.         if (ExitGameWindow) {
    48.  
    49.                         GUI.Box (new Rect (Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 200), "Exit To Main Menu?");
    50.                 }
    51.  
    52.        
    53.         // Make a button that can be clicked on.
    54.         // Debug.Log() creates text in your console.
    55.         // Application.LoadLevel(); Takes you to what ever scene that you tell it to. scene number goes in () after LoadLevel.
    56.         // if(ButtonYes) This will make it to where the yes button will not show when game is played.
    57.        
    58.         if (ButtonYes) {
    59.  
    60.             if (GUI.Button (new Rect (Screen.width / 2 -70, Screen.height / 2 -120, 150, 50), "Yes"))
    61.        
    62.        
    63.             // Takes you back to the Main Menu.
    64.             Application.LoadLevel(0);
    65.             Debug.Log ("Going Back To Main Menu");
    66.  
    67.         }
    68.        
    69.        
    70.        
    71.     }
    72. }

    Next thing I want to learn how to do is when this code happens I want the player to not move when I try and click the button that shows up and I want the game to pause. These two functions might do the same, but I have not started reading the Time.timescale scripts yet.
     
    Last edited: Apr 22, 2014
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    A few suggestions:
    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.      
    5.     public class ExitGame : MonoBehaviour {
    6.      
    7.          // this is the window for the exit yes button
    8.         public bool ExitGameWindow = false;
    9.      
    10.         // XXX(remove)XXX This is the Exit Yes button
    11.         // XXX(remove)XXX public bool ButtonYes = false;
    12.      
    13.        
    14.         // Use this for initialization
    15.         void Start () {
    16.             Screen.showCursor = false;
    17.         }
    18.        
    19.         // Update is called once per frame
    20.         void Update () {
    21.      
    22.             // This will show the mouse cursor when you hit Escape key and open the ExitGameWindow.
    23.      
    24.             if (Input.GetKeyDown(KeyCode.Escape)) { // <--- Use GetKeyDown.
    25.                 ExitGameWindow = !ExitGameWindow;
    26.                 // XXX(remove)XXX ButtonYes = true;
    27.                 Screen.showCursor = ExitGameWindow;
    28.                 Time.timeScale = ExitGameWindow ? 0 : 1;
    29.             }
    30.         }
    31.      
    32.      
    33.         // Void OnGUI() is the function for your Graphic user Interface (GUI)
    34.         void OnGUI() {
    35.            
    36.             // Make a backgroud for the button.
    37.             // This type of code will never allow the box with the buttons in it to change postion on screen no matter what screen size.
    38.             // As of now the if(ExitGameWindow) makes it so when you start game this window does not show.
    39.      
    40.      
    41.             if (ExitGameWindow) {
    42.      
    43.                 GUI.Box (new Rect (Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 200), "Exit To Main Menu?");
    44.  
    45.                 // Inside the window, make a button that can be clicked on.
    46.                 // Debug.Log() creates text in your console.
    47.                 // Application.LoadLevel(); Takes you to what ever scene that you tell it to. scene number goes in () after LoadLevel.
    48.                 // XXX(remove)XXX if(ButtonYes) This will make it to where the yes button will not show when game is played.
    49.            
    50.                 if (GUI.Button (new Rect (Screen.width / 2 -70, Screen.height / 2 -120, 150, 50), "Yes")) {
    51.            
    52.                     // Takes you back to the Main Menu.
    53.                     ExitGameWindow = false;
    54.                     Time.timeScale = 1;
    55.                     Application.LoadLevel(0);
    56.                     Debug.Log ("Going Back To Main Menu");
    57.      
    58.             }
    59.         }
    60.     }
    61.  
    • Use Input.GetKeyDown(), not Input.GetKey(). GetKeyDown() is only true when the player first presses Escape. GetKey() is down every single frame that Escape is held down.
    • In Update(), the changes above allow you to toggle the menu on and off by pressing the Escape key again.
    • Setting Time.timeScale to zero is a good way to pause the game. In Update(), the code sets timeScale to 0 if the exit window is up and 1 if the exit window is hidden.
    • You don't need ButtonYes. Just nest the button inside the if (ExitGameWindow) {...} block.
    • When you click the "Yes" button, it closes the window and sets timeScale back to 1.
    • Since you're only drawing the window if ExitGameMenu is true, it won't take up much CPU time at other times. It will take a little, however, since even an empty call to OnGUI() has a little overhead.
     
  3. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExitGame : MonoBehaviour {
    5.  
    6.     //This is the Variable for the ExitGameWindow.
    7.  
    8.     public bool ExitGameWindow = false;
    9.  
    10.  
    11.     //This defines a Variable for the MouseLook Script on the player.
    12.     MouseLook  playerLook;
    13.  
    14.     //This defines a Variable for the MouseLook Script on the playerCamera.
    15.     MouseLook playerCameraLook;
    16.  
    17.  
    18.  
    19.  
    20.    
    21.     // Use this for initialization
    22.     void Start () {
    23.  
    24.         //This will make it to where the mouse cursor will not show on start of game scene.
    25.  
    26.         Screen.showCursor = false;
    27.  
    28.         //This finds the MouseLook script and the First Person Controller for the player.
    29.  
    30.         playerLook = (MouseLook)GameObject.Find ("First Person Controller").GetComponent ("MouseLook");
    31.  
    32.         //This finds the MouseLook script and the First Person Controller for the playerCamera.
    33.  
    34.         playerCameraLook = (MouseLook)GameObject.Find ("Main Camera").GetComponent ("MouseLook");
    35.     }
    36.    
    37.     // Update is called once per frame
    38.     void Update () {
    39.  
    40.  
    41.         //This will show the mouse cursor when you hit Escape key and open the ExitGameWindow and the Yes Button.
    42.  
    43.         if (Input.GetKeyDown (KeyCode.Escape)) { //Hit ESC to Open Window
    44.             ExitGameWindow = !ExitGameWindow; //This code toggles the ExitGameWindow on/off
    45.             Screen.showCursor = !ExitGameWindow; //This code allows the mouse cursor to show up while the exit window is up.
    46.             Time.timeScale = ExitGameWindow ? 0 : 1; //This code sets timeScale to 0 if the exit window is up and 1 if the exit window is hidden.
    47.             playerLook.enabled = !playerLook.enabled; //This code makes it to where if you move the mouse nothing happens.
    48.             playerCameraLook.enabled = !playerCameraLook.enabled; //This code makes it to where if you move the mouse nothing happens.
    49.                 }
    50.  
    51.     }
    52.  
    53.  
    54.  
    55.     // Void OnGUI() is the function for your Graphic user Interface (GUI)
    56.     void OnGUI() {
    57.        
    58.         // Make a backgroud for the button.
    59.         // This type of code will never allow the box with the buttons in it to change postion on screen no matter what screen size.
    60.         // As of now the if(ExitGameWindow) makes it so when you start game this window does not show.
    61.  
    62.  
    63.         if (ExitGameWindow) {
    64.  
    65.                         GUI.Box (new Rect (Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 200), "Exit To Main Menu?"); //Creates the Main Menu Box.
    66.                 }
    67.  
    68.        
    69.         // Make a button that can be clicked on.
    70.  
    71.             if (GUI.Button (new Rect (Screen.width / 2 - 70, Screen.height / 2 - 120, 150, 50), "Yes")) { //Creates my Yes Button in the Window.
    72.  
    73.             ExitGameWindow = false; //Closes the ExitGameWindow.
    74.             Time.timeScale = 1; //Un pause the game
    75.             Application.LoadLevel (0); //Takes you back to Main Menu
    76.             Debug.Log ("Going Back To Main Menu"); //Types msg out in the console.
    77.                 }
    78.  
    79.        
    80.        
    81.        
    82.     }
    83. }
    I have added in your suggestions and also added more code in.

    Issue # 1: the Yes button is always active on the Screen. I Know it cause I had taken out the var ButtonYes.
    You made this comment: "You don't need ButtonYes. Just nest the button inside the if (ExitGameWindow) {...} block". I think your saying I need to combine the two somehow, correct?

    Fixed** I just was not reading you right. I went ahead and nested the Yes button in side the if{ExitGameWindow) {} code blocks lol Thanks for your suggestions!


    Issue # 2: My mouse is not active on screen after hitting esc.
    Fixed** Changed Screen.showCursor = !ExitGameWindow; to Screen.showCursor = ExitGameWindow;

    Also I have made heavy comments on my code. Please read through it and tell me if it all makes since to someone who did not write it.
     
    Last edited: Apr 21, 2014
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    It's coming along!

    Issue #1: Yes, combine the two by moving the "}" from line 66 above to line 78.

    Issue #2: Line 45 above should be Screen.showCursor = ExitGameWindow; (No "!". Was there a reason why you added it?)


    On lines 30 and 34, I suggest using the typed version of GetComponent:

    playerLook = GameObject.Find("First Person Controller").GetComponent<MouseLook>();​

    Better yet, always check for null. What if GameObject.Find() doesn't find the object?

    GameObject player = GameObject.Find("First Person Controller");
    playerLook = (player != null) ? player.GetComponent<MouseLook>() : null;

    Change lines 47-48 to:

    playerLook.enabled = !ExitGameWindow;
    playerCameraLook.enabled = !ExitGameWindow;

    This will ensure that they're always in sync with the state of the exit window.

    (Pardon my omission of [code ... /code] tags. I don't want it to add line numbers that might be confusing.)
     
  5. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29

    Issue # 2 It was a error on my part that I did not catch till after I posted that.

    On lines 30 and 34, I have to research this as I have only been studying C# for about 3 weeks. I understand what you said. If the game object is not found there would be a issue and the code would not work. But the way you wrote I need to understand it in code terms. I don't like to just copy and paste code without understanding it first.

    Change lines 47-48 to: I understand this and will change that as it goes with the rest of the code and it makes sense.

    My next task is to combine my 3 different scrips for each scene to 1 big one. IE Options, Credits, Main Menu.

    Thanks a ton, I have learned quite a bit today.
     
    Last edited: Apr 21, 2014
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Happy to help!
     
  7. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GUI_Menu : MonoBehaviour {
    5.  
    6.     //Variable Start
    7.  
    8.     public GUISkin customSkin;
    9.     private string CurrentMenu;
    10.     public float gSliderValue = 0.0f;
    11.     public float mSliderValue = 0.0f;
    12.  
    13.  
    14.  
    15.  
    16.  
    17.     //Variable End
    18.  
    19.     // Use this for initialization
    20.     void Start ()
    21.     {
    22.         CurrentMenu = "MainMenu";
    23.  
    24.         Screen.showCursor = true;
    25.    
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update ()
    30.     {
    31.    
    32.     }
    33.  
    34.     void ToMenu(string menu)
    35.     {
    36.         CurrentMenu = menu;
    37.     }
    38.  
    39.     void Awake()
    40.     {
    41.         DontDestroyOnLoad (transform.gameObject);
    42.     }
    43.  
    44.  
    45.     void OnGUI()
    46.     {
    47.         GUI.skin = customSkin;
    48.  
    49.         if (CurrentMenu == "MainMenu")
    50.                         MainMenu ();
    51.         if (CurrentMenu == "Options")
    52.                         Options ();
    53.         if (CurrentMenu == "Credits")
    54.                         Credits ();
    55.         //if (CurrentMenu == "GraphOptions")
    56.                         //GraphOptions ();
    57.  
    58.     }
    59.     private void MainMenu()
    60.     {
    61.         GUI.Box (new Rect(Screen.width / 2 -50, Screen.height / 2 -200, 100, 200), "Main Menu");
    62.  
    63.         if (GUI.Button (new Rect (Screen.width / 2 - 40, Screen.height / 2 - 170, 80, 20), "New Game"))
    64.         {
    65.             Debug.Log ("Loading New Game");
    66.             Application.LoadLevel (1);
    67.         }
    68.  
    69.         if (GUI.Button (new Rect (Screen.width / 2 -40, Screen.height /2 -150, 80, 20), "Options"))
    70.         {
    71.             Debug.Log ("Options");
    72.             ToMenu("Options");
    73.        
    74.         }
    75.  
    76.         if (GUI.Button (new Rect (Screen.width / 2 -40, Screen.height /2 -130, 80, 20), "Credits"))
    77.         {
    78.             Debug.Log ("Credits");
    79.             ToMenu("Credits");
    80.         }
    81.  
    82.         if (GUI.Button (new Rect (Screen.width / 2 -40, Screen.height /2 -110, 80, 20), "Exit"))
    83.         {
    84.             Debug.Log ("Exit");
    85.             Application.Quit();
    86.         }
    87.  
    88.  
    89.     }
    90.  
    91.     private void Options()
    92.     {
    93.         GUI.Box (new Rect(Screen.width / 2 -150, Screen.height / 2 -150, 300, 200), "Options Menu");
    94.  
    95.         if (GUI.Button (new Rect (Screen.width / 2 -150, Screen.height / 2 -150, 80, 40), "Go Back"))
    96.         {
    97.             Application.LoadLevel(0);
    98.             Debug.Log ("Going Back To Main Menu");
    99.         }
    100.  
    101.         GUI.Box (new Rect (Screen.width / 2 + 75, Screen.height / 2 - 100, 60, 20), gSliderValue.ToString ());
    102.         gSliderValue = GUI.HorizontalSlider(new Rect(Screen.width / 2 -50, Screen.height / 2 -95, 100, 30), gSliderValue, 0.0F, 10.0F);
    103.  
    104.         if (GUI.Button (new Rect (Screen.width / 2 - 145, Screen.height / 2 - 100, 90, 20), "Game Sound"))
    105.         {
    106.            
    107.         }
    108.  
    109.         GUI.Box (new Rect (Screen.width / 2 + 75, Screen.height / 2 - 55, 60, 20), mSliderValue.ToString ());
    110.         mSliderValue = GUI.HorizontalSlider(new Rect(Screen.width / 2 -50, Screen.height / 2 -50, 100, 30), mSliderValue, 0.0F, 10.0F);
    111.  
    112.         if (GUI.Button (new Rect (Screen.width / 2 - 145, Screen.height / 2 - 55, 90, 20), "Music Sound"))
    113.         {
    114.            
    115.         }
    116.  
    117.  
    118.     }
    119.  
    120.     private void Credits()
    121.     {
    122.         GUI.Box (new Rect(Screen.width / 2 -150, Screen.height / 2 -150, 300, 200), "Credits");
    123.  
    124.         if (GUI.Button (new Rect (Screen.width / 2 -150, Screen.height / 2 -150, 80, 40), "Go Back"))
    125.         {
    126.             Application.LoadLevel(0);
    127.             Debug.Log ("Going Back To Main Menu");
    128.         }
    129.  
    130.         GUI.Label(new Rect(Screen.width / 2 -140, Screen.height / 2 -100, 100, 20),"MegaTarre Dev");
    131.  
    132.     }
    133.  
    134.  
    135.  
    136. }
    When I change menus they start to over lap when I try to go back to the previous menu.

    I think it has to do with DontDestroyOnLoad (transform.gameObject); I may be missing something. I like to figure it out myself but a hint would be nice :)

    This maybe​

    ** If the object is a component or game object then its entire transform hierarchy will not be destroyed either.**


    Thanks!
     
    Last edited: Apr 22, 2014
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    You're right. Since it's set to DontDestroyOnLoad, you'll just keep accumulating more copies of it. You don't need to reload the level. Just call ToMenu("MainMenu"). For example:
    Code (csharp):
    1.  
    2.     private void Options()
    3.     {
    4.         GUI.Box (new Rect(Screen.width / 2 -150, Screen.height / 2 -150, 300, 200), "Options Menu");
    5.  
    6.         if (GUI.Button (new Rect (Screen.width / 2 -150, Screen.height / 2 -150, 80, 40), "Go Back"))
    7.         {
    8.             ToMenu("MainMenu");
    9.             Debug.Log ("Going Back To Main Menu");
    10.         }
    11.     ...
    12.  
    Also, read up on enum. Strings are dangerous because it's easy to have a typo. Enums are safer. For example:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class GUI_Menu : MonoBehaviour {
    6.  
    7.     //Variable Start
    8.  
    9.     public GUISkin customSkin;
    10.     public float gSliderValue = 0.0f;
    11.     public float mSliderValue = 0.0f;
    12.  
    13.     private enum Menus { Main, Options, Credits }  // <--- !!! USING ENUM INSTEAD OF STRING !!!
    14.     private Menus currentMenu = Menus.Main;
    15.  
    16.     //Variable End
    17.  
    18.      // Use this for initialization
    19.  
    20.     void Start ()
    21.    {
    22.         // Don't need this since we initialized it above:
    23.         // --- CurrentMenu = "MainMenu";
    24.  
    25.         Screen.showCursor = true;
    26.     }
    27.  
    28.     // Update is called once per frame
    29.  
    30.     ...
    31.  
    32.     void ToMenu(Menus menu)
    33.     {
    34.         currentMenu = menu;
    35.     }
    36.  
    37.     ...
    38.  
    39.     void OnGUI()
    40.     {
    41.         GUI.skin = customSkin;
    42.         switch (currentMenu) {
    43.             case Menus.Main:
    44.                 MainMenu();
    45.                 break;
    46.             case Menus.Options:
    47.                 Options();
    48.                 break;
    49.             case Menus.Options:
    50.                 Credits();
    51.                 break;
    52.         }
    53.     }
    54. ...
    55.  
     
  9. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    Yep I thought I needed to add DontDestroyOnLoad (transform.gameObject); but I was wrong. I deleted that 1 line of code and it works perfect now.

    I feel like every time I write a lot of code, there always a better way to do it. I just taught myself the string way lol. I guess I will look up ENUM now.

    thanks.
     
  10. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    Ok a new possible issue has come up. I have a logo on my Main menu scene. My options credits etc.. don't have this logo. But with the above code when I click a button that logo stays there. It's like it deleting the draw calls and just drawing the GUI for the scene I click on in the same scene. IE Options etc.. This has me thinking my code is not working right.

    PS: I am still using the String setup.

    If I use Application.LoadLevel (1); It changes scenes but no gui shows up.
     
    Last edited: Apr 22, 2014
  11. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    It'll help to trace through it in your head (or on paper) first to identify what's going to happen at each step.

    Application.LoadLevel() destroys all the objects in the current scene and loads the objects in the new scene. The exception is if you've marked an object with DontDestroyOnLoad; these objects will not be destroyed when you load a new scene.

    So if you mark your menu script object DontDestroyOnLoad and reload that same level, you'll get two copies of the object. So never reload that level. Or, alternatively, use the singleton pattern.

    You can take another approach by putting each menu in its own level and not marking them DontDestroyOnLoad. In this case, just load each level to show its menu.
     
  12. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GUI_Menu : MonoBehaviour {
    5.  
    6.     //Variable Start
    7.  
    8.     public GUISkin customSkin;
    9.     private string CurrentMenu;
    10.     public float gSliderValue = 0.0f;
    11.     public float mSliderValue = 0.0f;
    12.  
    13.     //Variable End
    14.  
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.         CurrentMenu = "MainMenu";
    19.  
    20.         Screen.showCursor = true;
    21.    
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update ()
    26.     {
    27.    
    28.     }
    29.  
    30.     void ToMenu(string menu)
    31.     {
    32.         CurrentMenu = menu;
    33.     }
    34.  
    35.     void Awake()
    36.     {
    37.  
    38.  
    39.     }
    40.  
    41.  
    42.     void OnGUI()
    43.     {
    44.         GUI.skin = customSkin;
    45.  
    46.         if (CurrentMenu == "MainMenu")
    47.                         MainMenu ();
    48.         if (CurrentMenu == "Options")
    49.                         Options ();
    50.         if (CurrentMenu == "Credits")
    51.                         Credits ();
    52.         if (CurrentMenu == "GraphOptions")
    53.                         GraphOptions ();
    54.         if (CurrentMenu == "LoadGameMenu")
    55.                         LoadGameMenu ();
    56.  
    57.     }
    58.     private void MainMenu()
    59.     {
    60.         GUI.Box (new Rect(Screen.width / 2 -50, Screen.height / 2 -200, 100, 200), "Main Menu");
    61.  
    62.         if (GUI.Button (new Rect (Screen.width / 2 - 40, Screen.height / 2 - 170, 80, 20), "New Game"))
    63.         {
    64.             Debug.Log ("Loading New Game");
    65.             Application.LoadLevel (1);
    66.         }
    67.  
    68.         if (GUI.Button (new Rect (Screen.width / 2 - 40, Screen.height / 2 - 150, 80, 20), "Load Game"))
    69.         {
    70.             Debug.Log("Loading Game");
    71.             ToMenu("LoadGameMenu");
    72.            
    73.  
    74.         }
    75.  
    76.         if (GUI.Button (new Rect (Screen.width / 2 -40, Screen.height /2 -130, 80, 20), "Options"))
    77.         {
    78.             Debug.Log ("Options");
    79.             ToMenu("Options");
    80.        
    81.         }
    82.  
    83.         if (GUI.Button (new Rect (Screen.width / 2 -40, Screen.height /2 -110, 80, 20), "Credits"))
    84.         {
    85.             Debug.Log ("Credits");
    86.             ToMenu("Credits");
    87.         }
    88.  
    89.         if (GUI.Button (new Rect (Screen.width / 2 -40, Screen.height /2 -90, 80, 20), "Exit"))
    90.         {
    91.             Debug.Log ("Exit");
    92.             Application.Quit();
    93.         }
    94.  
    95.  
    96.     }
    97.  
    98.     private void LoadGameMenu()
    99.     {
    100.         GUI.Box (new Rect(Screen.width / 2 -150, Screen.height / 2 -150, 300, 200), "Saves");
    101.     }
    102.  
    103.     private void Options()
    104.     {
    105.         GUI.Box (new Rect(Screen.width / 2 -150, Screen.height / 2 -150, 300, 200), "Options Menu");
    106.  
    107.         if (GUI.Button (new Rect (Screen.width / 2 -150, Screen.height / 2 -150, 80, 40), "Go Back"))
    108.         {
    109.             ToMenu("MainMenu");
    110.             Debug.Log ("Going Back To Main Menu");
    111.         }
    112.  
    113.         GUI.Box (new Rect (Screen.width / 2 + 75, Screen.height / 2 - 100, 60, 20), gSliderValue.ToString ());
    114.         gSliderValue = GUI.HorizontalSlider(new Rect(Screen.width / 2 -50, Screen.height / 2 -95, 100, 30), gSliderValue, 0.0F, 10.0F);
    115.  
    116.         if (GUI.Button (new Rect (Screen.width / 2 - 145, Screen.height / 2 - 100, 90, 20), "Game Sound"))
    117.         {
    118.            
    119.         }
    120.  
    121.         GUI.Box (new Rect (Screen.width / 2 + 75, Screen.height / 2 - 55, 60, 20), mSliderValue.ToString ());
    122.         mSliderValue = GUI.HorizontalSlider(new Rect(Screen.width / 2 -50, Screen.height / 2 -50, 100, 30), mSliderValue, 0.0F, 10.0F);
    123.  
    124.         if (GUI.Button (new Rect (Screen.width / 2 - 145, Screen.height / 2 - 55, 90, 20), "Music Sound"))
    125.         {
    126.            
    127.         }
    128.  
    129.  
    130.     }
    131.  
    132.     private void GraphOptions()
    133.     {
    134.     }
    135.  
    136.     private void Credits()
    137.     {
    138.         GUI.Box (new Rect(Screen.width / 2 -150, Screen.height / 2 -150, 300, 200), "Credits");
    139.  
    140.         if (GUI.Button (new Rect (Screen.width / 2 -150, Screen.height / 2 -150, 80, 40), "Go Back"))
    141.         {
    142.             ToMenu("MainMenu");
    143.             Debug.Log ("Going Back To Main Menu");
    144.         }
    145.  
    146.         GUI.Label(new Rect(Screen.width / 2 -140, Screen.height / 2 -100, 100, 20),"MegaTarre Dev");
    147.  
    148.     }
    149.  
    150.  
    151.  
    152. }
    With the code that I have everything is done in just 1 scene correct? I am not loading a new scene. Every time options, credits, etc.. is clicked it just deletes what GUI is there and draws the GUI code that is attached to that button. I think this is the reason why my Logo that is a GUI texture atm and my GUI text is showing up not matter what button is clicked. I think I need to add code to make it only show up when the main menu GUI code is called. Please correct me if I am wrong. Also is it ok if I do it this way? Later when I added functions to the options. GUI will I be able to save what options is changed if I keep going the way I am?
     
  13. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Yes, it's okay to do it that way, and you'll be able to save options. To show the logo only in the main menu, use GUI.DrawTexture() instead of a GUI texture. Put the GUI.DrawTexture() call in your MainMenu() method.

    If this script gets too big or complicated, you could redesign it with one scene per menu. When you load the Credits scene, it shows only the Credits menu. If you click Main Menu in the Credits scene, is loads the Main Menu scene, which shows only the Main Menu. You'll have more scenes this way, but the script attached to each scene is much smaller.
     
  14. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    Yep I went ahead and just added the code for the texture It now only shows up in the MainMenu method. Thank alot for all the help,
     
  15. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Happy to help!