- Home /
 
               Question by 
               Doneyes · Nov 23, 2012 at 01:35 AM · 
                javascriptguibuttonfunction  
              
 
              How do I make the buttons work on this GUI?
I have a script that creates buttons, but how do I get them to respond?
 #pragma strict
 var buttonName :String;
 private var act :boolean;
 var closeOnButton :boolean = true; //Should it close again when you press b?
 function Start () {
 
 }
 
 function Update () {
 if(Input.GetKeyDown(buttonName)){
 if(closeOnButton){
 if(act){
 act = false;
 }else act = true;
 }else{
 act = true;
 }
 }
 }
 
 function OnGUI(){
 if(act){
 GUI.Box(Rect(Screen.width / 6, Screen.height / 6, Screen.width / 1.5, Screen.height / 1.5), ""); //Background Box
 GUI.Button (Rect(Screen.width / 6 + 15, Screen.height / 3.5 + 15, Screen.width / 3 - 30, Screen.height / 4 - 15), "Helmets"); //Button one
 GUI.Button (Rect(Screen.width / 6 + 15, Screen.height / 1.8 + 15, Screen.width / 3 - 30, Screen.height / 4 - 15), "Torso"); //Button two
 GUI.Button (Rect(Screen.width / 2 + 15, Screen.height / 1.8 + 15, Screen.width / 3 - 30, Screen.height / 4 - 15), "Legs"); //Button three
 GUI.Button (Rect(Screen.width / 2 + 15, Screen.height / 3.5 + 15, Screen.width / 3 - 30, Screen.height / 4 - 15), "Sets"); //Button four
 }
 }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Democre · Nov 23, 2012 at 01:47 AM
something went weird when you pasted the code, but you should put each of those buttons in an if block. The if will return true when it is pressed. example:
 function OnGUI(){
     if(act){
         GUI.Box(Rect(Screen.width / 6, Screen.height / 6, Screen.width / 1.5, Screen.height / 1.5), ""); //Background Box
         if(GUI.Button (Rect(Screen.width / 6 + 15, Screen.height / 3.5 + 15, Screen.width / 3 - 30, Screen.height / 4 - 15), "Helmets")) { //Button one
             //set the flag for helmets gui or call whatever to do something with helmets
         }
         //...
     }
 
     if(helmet) {
         //draw helmet stuff
     }
 }
Answer by greatestprez · Nov 23, 2012 at 03:56 AM
Simply put the button creation code inside an if statement. example:
 if (GUI.Button (Rect(Screen.width / 6 + 15, Screen.height / 3.5 + 15, Screen.width / 3 - 30, Screen.height / 4 - 15), "Helmets")) {
 
       //insert code to react to the press here
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                