Search Unity

I need a GUI button to disappear. Help please

Discussion in 'Scripting' started by SirMarley, Jul 30, 2014.

  1. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    Hey there,

    I wonder if there is anyway I can make a GUI button to disappear.

    I have a menu with 6 of them, and each one will show you some options on the same screen of the menu, so I wonder if when I click on one of the main buttons, the options from the other one I pressed earlier can disappear?

    I have tried using a bool, but it did not work.

    Here is what I have:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Options : MonoBehaviour {
    5.  
    6.     public Texture MainMenuTexture;
    7.     public bool sound = false;
    8.     public bool highScores = false;
    9.     public bool store = false;
    10.     public bool gallery = false;
    11.     public bool invite = false;
    12.     public bool credits = false;
    13.  
    14.     void OnGUI()
    15.     {
    16.         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), MainMenuTexture);
    17.  
    18.             if (GUI.Button(new Rect(0, Screen.height * .17f, Screen.width * .2f, Screen.height * .1f), "SOUND"))
    19.             {
    20.                 sound = true;
    21.             }
    22.             if (GUI.Button(new Rect(0, Screen.height * .30f, Screen.width * .2f, Screen.height * .1f), "HIGH SCORES"))
    23.             {
    24.                 highScores = true;
    25.             }
    26.             if (GUI.Button(new Rect(0, Screen.height * .43f, Screen.width * .2f, Screen.height * .1f), "STORE"))
    27.             {
    28.                 store = true;
    29.             }
    30.             if (GUI.Button(new Rect(0, Screen.height * .56f, Screen.width * .2f, Screen.height * .1f), "GALLERY"))
    31.             {
    32.                 gallery = true;
    33.             }
    34.             if (GUI.Button(new Rect(0, Screen.height * .69f, Screen.width * .2f, Screen.height * .1f), "INVITE"))
    35.             {
    36.                 invite = true;
    37.             }
    38.             if (GUI.Button(new Rect(0, Screen.height * .82f, Screen.width * .2f, Screen.height * .1f), "CREDITS"))
    39.             {
    40.                 credits = true;
    41.             }
    42.  
    43.         }
    44.  
    45.         if (sound)
    46.         {
    47.             highScores = false;
    48.             store = false;
    49.             gallery = false;
    50.             invite = false;
    51.             credits = false;
    52.                 if (GUI.Button(new Rect(Screen.width * .3f, Screen.height * .17f, Screen.width * .3f, Screen.height * .1f), "YES"))
    53.                 {
    54.  
    55.                 }
    56.                 if (GUI.Button(new Rect(Screen.width * .7f, Screen.height * .17f, Screen.width * .3f, Screen.height * .1f), "NO"))
    57.                 {
    58.  
    59.                 }
    60.         }
    61.         if (highScores)
    62.         {
    63.             sound = false;
    64.             store = false;
    65.             gallery = false;
    66.             invite = false;
    67.             credits = false;
    68.         }
    69.     }
    70. }
    71.  
    Thanks!
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    use an enum to decide which button it should render.
    ex:
    Code (CSharp):
    1. public enum ScreenState
    2.     {
    3.         Main,
    4.         Customize,
    5.         Options
    6.     }
    7.  
    8.     public ScreenState CurrentState;
    9.  
    10.     void OnGUI()
    11.     {
    12.         switch (CurrentState)
    13.         {
    14.             case ScreenState.Main:
    15.                 if (GUI.Button(new Rect(), "Start Game"))
    16.                 {
    17.                     //...
    18.                 }
    19.                 if (GUI.Button(new Rect(), "Customize Character"))
    20.                 {
    21.                     //...
    22.                 }
    23.                 if (GUI.Button(new Rect(), "Options"))
    24.                 {
    25.                     //...
    26.                 }
    27.                 break;
    28.             case ScreenState.Customize:
    29.                 if (GUI.Button(new Rect(), "Change appearance"))
    30.                 {
    31.                     //...
    32.                 }
    33.                 if (GUI.Button(new Rect(), "Back"))
    34.                 {
    35.                     //...
    36.                 }
    37.                 break;
    38.             case ScreenState.Options:
    39.                 if (GUI.Button(new Rect(), "FOV"))
    40.                 {
    41.                     //...
    42.                 }
    43.                 if (GUI.Button(new Rect(), "Pointer Speed"))
    44.                 {
    45.                     //...
    46.                 }
    47.                 if (GUI.Button(new Rect(), "Back"))
    48.                 {
    49.                     //...
    50.                 }
    51.                 break;
    52.         }
    53.     }