- Home /
Gui Toggle help
I'm working on a main menu. I'm trying to get a bunch of toggle options in the settings, but no matter which one you toggle, it only toggles on and off the first one.
Here is the script: enter code herestatic var Quality : boolean = false; private var displayLabel : boolean; private var toggleTxt : boolean = false; private var toggleTxt1 : boolean = false;
function Start(){
displayLabel = false;
}
function OnGUI () {
GUI.skin = customSkin;
GUI.Box(new Rect(20,20,200,100),"");
//You can change name for your box (before parentheses & after last comma)//Also you can resize the box by changing the numbers(first two numbers for the position & the last two numbers for size the GUI objects)
if(GUI.Button(Rect(25,25,70,20),"Join")){
Debug.Log("You clicked that button");
}
if(GUI.Button(Rect(25,50,70,20),"Settings")){
displayLabel = true;
}
if(displayLabel){
GUI.Box(Rect (500,20,200,100),"Settings");
}
if(displayLabel){
toggleTxt = GUI.Toggle(Rect(500, 50, 200, 100), toggleTxt, "Fastest");
if(toggleTxt){
RenderSettings.ambientLight = Color.red;
Quality = true;
}
else
{
toggleTxt = false;
RenderSettings.ambientLight = Color.clear;
}
}
if(displayLabel){
toggleTxt1 = GUI.Toggle(Rect(500, 65, 200, 100), toggleTxt1, "Fast");
if(toggleTxt1){
RenderSettings.ambientLight = Color.red;
Quality1 = true;
}
else
{
toggleTxt1 = false;
RenderSettings.ambientLight = Color.clear;
}
}
//That's how to create a GUI button//(The same)
GUI.Label(new Rect(Screen.width - 500,0,100,50), "Task Force-8");
//You can change name for this label by typing words (before parentheses & after last comma)
}
Answer by iwaldrop · Mar 29, 2013 at 04:07 AM
You shouldn't mix logic in with GUI stuff. Setting the flags toggleTxt and toggleTxt1 is all you should be doing in there, then checking the flags in a loop somewhere (Update is doable, but not ideal).
In fact, this is not a very good way of going about it at all. The reason is that you're setting flags instead of kicking off a method to handle the changes. Ideally you have buttons that trigger one method that handles cases of settings, thusly;
public enum Quality { Low, Med, High };
public Quality quality = Quality.Med;
void Awake()
{
QualityToggle(quality);
}
void OnGUI()
{
if (GUI.Button(someRect, "Low"))
QualityToggle(Quality.Low);
if (GUI.Button(someRect, "Medium"))
QualityToggle(Quality.Med);
}
void QualityToggle(Quality quality)
{
switch (quality)
{
case Quality.Low:
this.quality = quality;
// do low quality stuff
break;
case Quality.Med...
}
}
Answer by Chronos-L · Mar 29, 2013 at 04:16 AM
I was going to comment on the way you structure your logic, but @iwaldrop got ahead of me. iwaldrop also provided a very good way to define the level of quality, by using a enum.
The reason your toggle is not behaving properly is you have define a unusually large Rect()
for the GUI.Toggle()
Change it to from these:
toggleTxt = GUI.Toggle(Rect(500, 50, 200, 100), toggleTxt, "Fastest");
...
toggleTxt1 = GUI.Toggle(Rect(500, 65, 200, 100), toggleTxt1, "Fast");
To something like these :
toggleTxt = GUI.Toggle(Rect(500, 50, 200, 25), toggleTxt, "Fastest");
...
toggleTxt1 = GUI.Toggle(Rect(500, 75, 200, 25), toggleTxt1, "Fast");
Pay attention to the position and the size of the Rect()
, in your original code, the Rect
for the first toggle overlaps the second toggle; you thought you were pressing on the second toggle but you are affecting the first.
Your answer
Follow this Question
Related Questions
Background color and structure disappear when enabling GUI Canvas 0 Answers
Re Enable C# script on camera 1 Answer
Close my GUI button by repressing the same Hot-key. 3 Answers
Stand alone player 0 Answers
Make this GUI Bigger? 1 Answer