- Home /
 
why doesnt my script turn off on toggle?
I made a basic GUI that I want to turn on and off when I press the Key "E"....but the problem is that it doesn't toggle!!! I made a public Boolean called "MenuState" this defined the state if the GUI is on or off ...when I turn it on and off manually everything works but when I press the "E" key nothing happens....please some one help me ....tnx.
 using UnityEngine;
 using System.Collections;
 
 public class guiskin : MonoBehaviour
 {
     //variables start*********************************************************************
     public GUISkin thisOrangeGUISkin;
     public bool MenuState = false;
     private Rect rctWindow1;
     private Rect rctWindow2;
     private Rect rctWindow3;
     private Rect rctWindow4;
     private bool blnToggleState = false;
     private float fltSliderValue = 0.5f;
     private float fltScrollerValue = 0.5f;
 //    private Vector2 scrollPosition = Vector2.zero;
     //variables end***********************************************************************
 
 
     void update(){
         if(Input.GetKeyDown(KeyCode.E)){
             MenuState = true;
         }
         else{MenuState = false;}
         
     }
     
     
     void Awake()
     {
         rctWindow1 = new Rect(20, 20, 320, 400);
 }
     
     
 
         
     void OnGUI()
     {
         if(MenuState == true){
         GUI.skin = thisOrangeGUISkin;
                 rctWindow1 = GUI.Window(0, rctWindow1, DoMyWindow, "OPTIONS", GUI.skin.GetStyle("window"));
         }}
     
     
         
     
         
         void DoMyWindow(int windowID)
     {
         GUILayout.BeginVertical();
         GUILayout.Label("Im a Label");
         GUILayout.Space(8);
         GUILayout.Button("Im a Button");
         GUILayout.TextField("Im a textfield");
         GUILayout.TextArea("Im a textfield\nIm the second line\nIm the third line\nIm the fourth line");
         blnToggleState = GUILayout.Toggle(blnToggleState, "Im a Toggle button");
         GUILayout.EndVertical();
         GUILayout.BeginVertical();
         //Sliders
         GUILayout.BeginHorizontal();
         fltSliderValue = GUILayout.HorizontalSlider(fltSliderValue, 0.0f, 1.1f, GUILayout.Width(128));
         fltSliderValue = GUILayout.VerticalSlider(fltSliderValue, 0.0f, 1.1f, GUILayout.Height(50));
         GUILayout.EndHorizontal();
         //Scrollbars
         GUILayout.BeginHorizontal();
         fltScrollerValue = GUILayout.HorizontalScrollbar(fltScrollerValue, 0.1f, 0.0f, 1.1f, GUILayout.Width(128));
         fltScrollerValue = GUILayout.VerticalScrollbar(fltScrollerValue, 0.1f, 0.0f, 1.1f, GUILayout.Height(90));
         GUILayout.Box("Im\na\ntest\nBox");
         GUILayout.EndHorizontal();
         GUILayout.EndVertical();
         GUI.DragWindow();
     }
     
 }
 
              Answer by aldonaletto · Apr 17, 2012 at 02:29 AM
You've written update, in lower case, but the correct name is Update. Furthermore, this code only sets MenuState to true during the frame where E is pressed. Toggle the variable this way:
    void Update(){
       if(Input.GetKeyDown(KeyCode.E)){
         MenuState = !MenuState; // toggle MenuState
       }
    }
 
              Your answer
 
             Follow this Question
Related Questions
Toggle GuiTexture 1 Answer
Can I have toggle buttons in a selection grid? 1 Answer
How does GUI.Toggle work? 2 Answers
Put toggle label on left side of button and hover 1 Answer
Is there a way of using just basic gui.Buttons as toggles? 1 Answer