- Home /
Toggle Switch
Hey i have a little problem and i hope someone can help me.
I want to make a list of GUI.Toggle and when you have a toggle on and then click on another one the active one should turn off.
I tried something and it kind of worked:
toggletest=GUI.Toggle(Rect(200,200,50,50),toggletest,"tes1");
toggletest2=GUI.Toggle(Rect(270,200,50,50),toggletest2,"tes2");
if(toggletest)
toggletest2=false;
if(toggletest2)
toggletest=false;
It only works when you have the second one selected and then click on the first one.
If you know how to fix the code above or if you know another, better way to do it I would be happy to read it. Help is much appreciated.
I Have An Toggle Created in $$anonymous$$y Editor. So Iam Not Using the OnGUI() Function And This Function Seems Impossible Cause When I Press One Toggle I want To $$anonymous$$ake The Other Toggle Off. And I Dont Have One Or Two Toggles So Its Pretty Tough Please Help $$anonymous$$e. Heres The Code:
public GameObject[] comtb;
void Update()
{
comtb = GameObject.FindGameObjectsWithTag ("components");
foreach(GameObject compnnts in comtb)
{
if (compnnts.GetComponent<Toggle> ()) {
compnnts.GetComponent<Toggle> ().isOn = SetOneCheck;
}
}
}
bool SetOneCheck()
{
foreach(GameObject compnnts in comtb)
{
compnnts.GetComponent<Toggle> ().isOn = false;
}
return true;
}
Answer by ExTheSea · May 26, 2012 at 05:28 PM
I found it out myself:
//in OnGUI(){
if(GUI.Toggle(Rect(200,200,50,50),toggletest,"tes1")) toggletest=setMeOnly();
if(GUI.Toggle(Rect(270,200,50,50),toggletest2,"tes2")) toggletest2=setMeOnly();
...}
function setMeOnly():boolean{
toggletest = toggletest2 = false;
return true;
}
Still if someone knows an even better way then just post it as an answer.
Answer by Xaurrien · May 31, 2012 at 03:11 PM
My way to do it :
if(GUI.Toggle(Rect(200,200,50,50),toggletest,"tes1")) {
toggletest = true;
toggletest2 = false;
}
if(GUI.Toggle(Rect(270,200,50,50),toggletest2,"tes2")) {
toggletest = false;
toggletest2 = true;
}
This is nearly the same thing that i'm using only it will take up more space when there will be more GUI.Toggle's.
The question was closed 4 days ago but still thanks for your answer as you are new to unity answers.
Answer by JeanClaude2010 · May 31, 2012 at 05:04 PM
HI everybody,
I'm facing the same problem. The thing is I have tried each of your solutions, and searched for it, but I can't manage to have it working in C# oO
Any help greatly appreciated !
What is your problem?? Which error do you get? I'm not used to work with C# in unity but from what i know it should be quite similar except of function being void in c. Is it necessary that it is in C?
Look at my new answer. I converted my code in C but i'm not 100 percent sure it willl work. Give it a try.
Answer by ExTheSea · May 31, 2012 at 06:05 PM
This is how i think my code would look like in C. I did this for jeanclaude2010
Hope this works because like i said i'm not used to work with C
public bool toogletest1 = false;
public bool toogletest2 = false;
void OnGUI(){
if(GUI.Toggle(new Rect(200,200,50,50),toggletest,"tes1")) toggletest=setMeOnly();
if(GUI.Toggle(new Rect(270,200,50,50),toggletest2,"tes2")) toggletest2=setMeOnly();
}
void setMeOnly():bool{ //not entirely sure if bool works maybe its boolean
toggletest = toggletest2 = false;
return true;
}
Answer by JeanClaude2010 · May 31, 2012 at 07:22 PM
Works perfectly, I had forgotten to use public variables. Many thanks !