- Home /
Two toggles, only one can be on, both can be off
Hi I'm having a problem with creating two toggles where only one can be on, if one is pressed the other switches off.
i am using the following code
if (toggleVert == true){
toggleBrie = false;
}
if (toggleBrie == true){
toggleVert = false;
}
it seems to work one way if i click on toggleBrie, toggleVert switches off, but if toggleBrie is on toggleVert will not switch on and switch other off.
any help would be appreciated.
taran
A couple questions: how are you changing these values, and are you using C# or JS?
Answer by burnumd · Nov 23, 2010 at 02:46 PM
Let's look at the flow of your code. First you check if toggleVert is true. If it is, you change toggleBrie to false. When you get to the next statement "if (toggleBrie == true)
" you've either just set toggleBrie to be false or it was not true to begin with. So you'll never fall into that expression. The better way to handle this problem is to determine this logic when one or the other is changed rather than, as I'm guessing you're doing, in Update/OnGUI/etc.
function SetVert (state : boolean) { if (state) { SetBrie (false); } toggleVert = state; }
function SetBrie (state : boolean) { if (state) { SetVert (false); } toggleBrie = state; }
And only use those functions to change the state of those values.
So, if you want to change the value with a GUI toggle element, just send the result of the Toggle function as the parameter.
function OnGUI ()
{
SetVert (GUILayout.Toggle (toggleVert, "Vert"));
SetBrie (GUILayout.Toggle (toggleBrie, "Brie"));
}
For C# users, you can use properties to get more natural-looking GUI code.
As a side note, when you're testing against booleans, you can simply test "if (toggleVert)
," the "== true
" is redundant.
O$$anonymous$$ i understand running a function that takes care of it, but how do I run these functions when a toggle is pressed, as I am having problems updating my scene, the toggle does not seem to update or run anything. (im using javascript)
Awesome mate that worked a charm, I would never have thought of doing that , passing the result of the toggle to a function. I clearly have a lot to learn.
Answer by Slem · Nov 23, 2010 at 02:38 PM
Where do you set this? In GUI? I would set the other value just after setting one. Like so(if set in GUI, but it applies):
toggleVert = GUILayout.Toggle(toggleVert);
if(GUI.changed)
{
toggleBrie = !toggleVert;
}
//and same of toggleBrie below.
Answer by bedford · Sep 24, 2012 at 09:45 AM
This has been asked a while ago, I know, but I found an easier way than the one proposed and it may help other people looking for a solution (I had many toggles on same group):
//declaration
private bool q1a;
private bool q1b;
private bool q1c;
void OnGUI(){
change1a(GUI.Toggle(new Rect(10,10,10,10),q1a,""));
change1b(GUI.Toggle(new Rect(30,10,10,10),q1b,""));
change1c(GUI.Toggle(new Rect(50,10,10,10),q1c,""));
}
private void change1a(bool newvalue){
if(newvalue){
q1b = false;
q1c = false;
}
q1a = newvalue;
}
private void change1b(bool newvalue){
if(newvalue){
q1a = false;
q1c = false;
}
q1b = newvalue;
}
private void change1c(bool newvalue){
if(newvalue){
q1a = false;
q1b = false;
}
q1c = newvalue;
}
Hope this help !