- Home /
GUI Button Question
Hello, the question i have is can you turn off certain parts of a script, for example i have 2 GUI Buttons but while ones on i dont want the other one one, but i dont want to make 2 separate scripts is there anyway to make it so that while ones on the the other cant be on, even thought there in the same script?
Answer by OperationDogBird · Sep 05, 2012 at 05:40 AM
Sure thats not a problem. There are a couple of approaches to this. Depending on the depth of the script you will favor one over the other. Firstly you could use a boolean method
var showSetOne:boolean;
function OnGUI()
{
if(showSetOne)
{
if(GUI.button(.....))//Do something
if(GUI.button(.....))//Do something
}
else
{
if(GUI.button(.....))//Do something
if(GUI.button(.....))//Do something
}
}
Now if your menu/gui setup is much more complex than just have 2 dimensions, you should use a switch case approach
var menu:int;
function OnGUI()
{
switch(menu)
{
case 0:
//ShowGUIForGroupZero
if(GUI.Button(.....))menu=1;
if(GUI.Button(.....))menu=2;
if(GUI.Button(.....))menu=3;
break;
case 1:
//ShowGUIForGroupOne
if(GUI.Button(.....))menu=2;
break;
case 2:
//ShowGUIForGroupTwo
if(GUI.Button(.....))menu=3;
break;
}
//Anything outside of the switch-case with be shown regardless of menu
}
ohhh okay i see so this would work? btw i program in C#
public bool showSetOne;
public bool showSetTwo
function OnGUI()
{ if(showSetOne)
{
if(GUI.button(.....))//Do something
if(GUI.button(.....))//Do something
}
else
{
if(GUI.button(.....))//Do something
if(GUI.button(.....))//Do something
}
if (showSetTwo)
{
if(GUI.button(.....))//Do something
if(GUI.button(.....))//Do something
}
else
{
if(GUI.button(.....))//Do something
if(GUI.button(.....))//Do something
}
}
For more than one set it would be better to use the switch case version since you now have 4 different scenarios( if(1)else if(2)else) ). The switch case is very easy to expand upon and will cause less clutter in the script since you will not need to make sure different booleans are at the proper value. Heres the C# version of switch, practically the same.
public int menu = 0;
void OnGUI()
{
switch(menu)
{
case 0:
//Do Some $$anonymous$$enu Zero Stuff
break;
}
}
Wait im alittle confused sry. could you explain how this code will disable one GUI button while the other is active
the gui will ONLY run in one case at a time. Thus: if menu = 0, the gui inside of case 0 in the switch will run and none of the other cases will meet the switches condition, its the same as saying
if(menu==0)
{
//Run this code only if menu is 0
}
if(menu==1)
{
//Run this code only if menu is 1
}
etc..
etc..
Very very handy when you have more than 2 conditions. Since we have the variable menu at hand, we dont need to change boolean values to true and false because the switch does all that for us. Plus later down the line if you need to add a new 'set' / menu of gui items, you can just make a new case for the switch : "if menu = x" / case x: //new menu
ohhh okay and when you click the next button it switches the menu?
Answer by Anusha · Sep 05, 2012 at 06:56 AM
i am guessing what you want to do is differentiate between your home page GUI [play btn,options btn, settings btn]and your in scene GUI[score,time]... so that when one shows other dosent and you want to do this in one script.
here's what you do:
private bool menu;
void Start()
{
menu = true;
}
void OnGUI()
{
if(menu)
{
//your homepage GUI goes here
if(GUI.Button(new Rect(10,10,10,10),"PLAY"))
{
menu = false;
}
}
else
{
//your scene GUI goes here
//make menu = true when u want to display homepage GUI again.
}
}
That didnt really work for me since i needed 2 buttons so i took some of what you wrote and added some stuff but it doesent seem to be working right. could you see if i did anything wrong
using UnityEngine;
using System.Collections;
public class BUTTONS2 : $$anonymous$$onoBehaviour {
private bool menu;
// Use this for initialization
void Start () {
menu = false;
}
// Update is called once per frame
void Update () {
if(menu = false){
GetComponent().enabled = true; } if(menu = true){
GetComponent().enabled = false; } }
void OnGUI(){
if(menu){
if(GUI.Button(new Rect(100,10,100,100),"PLAY2")) { menu = false; } } &else { menu = true; } } }
$$anonymous$$enu would be a integer not a boolean. take a look at my above stated comments in both my answer and this answer. Please answer in my answer since my answer is the one you are following.
okay sry and in my last example i did in your answer did i do it right
@OperationDogBird: Sir i did not say my answer was best or optimal... i just took an example and a very simple one , so that Robomaster could get an idea about how to go about things. it was just a simplest solution... about the options menu and all... IF they are present then i am sure by the time a devoloper reaches that far in coding he will be able to implement the best code.... i was just helping in getting him a headstart....in a simple way...
@Robomaster: when you use a condition you are to use '==' not '=' so if(menu == false){}... not if(menu = false){} also if its a boolean just if(menu){ }; is enough....
thats the mistake in the above code you pasted.
Answer by Anusha · Sep 05, 2012 at 08:51 AM
using UnityEngine;
using System.Collections;
public class BUTTONS2 : MonoBehaviour {
private bool menu;
// Use this for initialization
void Start () {
menu = false;
}
// Update is called once per frame
void Update ()
{
}
void OnGUI()
{
if(menu)
{
if(GUI.Button(new Rect(100,10,100,100),"PLAY2"))
{
menu = false;
GetComponent().enabled = true;
}
}
else
{
if(GUI.Button(new Rect(100,10,100,100),"PLAY1"))
{
menu = true;
GetComponent().enabled = false;
}
}
}
}
your code in a better way... no need update...your just redoing what can be done in OnGUI()....
Your answer
Follow this Question
Related Questions
How to add intermediate tooltip to GUI button? 1 Answer
GUI Button Jagged Edges JavaScript 0 Answers
Automatic Wrap 2 Answers
GUI Button sound problem, don´t work 1 Answer
GUI.button Texture shows up in Unity but doesn't show on android device. 1 Answer