- Home /
Gui toggle active inactive states
Hi im trying to create a GUI button that controls if an object is active or not.
So far I have done this using the following code but unity is calculating these lines so fast that the object will flick betweeen active and inactive.
if (GUI.Button (new Rect(Screen.width/2 - GUI2_button1_width/2 - GUI2_button1_flatspace, Screen.height/2 - GUI2_button1_height/2 - GUI2_button1_vertspace, GUI2_button1_width, GUI2_button1_height), "Soil type survey") && Soil_Survey.active == false)
{
print ("You clicked the button!");
Soil_Survey.active = true;
}
else if(Soil_Survey.active == true)
{
Soil_Survey.active = false;
}
How do overcome this?
Answer by melonman · Mar 01, 2012 at 02:14 PM
I figured it out your code is pretty much spot on except I needed to take out the last part of the code in the top line which states "&& Soil_Survey.active == false"
So the final revised piece which works and your quite right is......
if (GUI.Button (new Rect(Screen.width/2 - GUI2_button1_width/2 - GUI2_button1_flatspace, Screen.height/2 - GUI2_button1_height/2 - GUI2_button1_vertspace, GUI2_button1_width, GUI2_button1_height), "Soil type survey"))
{
if(Soil_Survey.active == false)
{
Soil_Survey.active = true;
}
else if(Soil_Survey.active == true)
{
Soil_Survey.active = false;
}
}
Haha I guess I was a lil late in posting this.
Yeah I just seen that aswell sorry guys
and thank you both for your speedy replies.
$$anonymous$$ Franken I just tried to click the tick on your post and it wouldn't let me tick two so apologies :(
Answer by Dreamside · Mar 01, 2012 at 12:15 PM
after assigning Soil_Survey=true; statement you check
else if(Soil_Survey.active==true) { Soil_Survey.active=false; }
Soil_Survey.active is always toggle fast because you assign it to true,and then check if it is true.If true you assign it to false.After clicking it becomes true and immediately again false.
What you should do is:
if (GUI.Button (new Rect(Screen.width/2 - GUI2_button1_width/2 - GUI2_button1_flatspace, Screen.height/2 - GUI2_button1_height/2 - GUI2_button1_vertspace, GUI2_button1_width, GUI2_button1_height), "Soil type survey") && Soil_Survey.active == false)
{
if(Soil_Survey.active==true)
{
Soil_Survey.active=false;
}
else
{
Soil_Survey.active=true;
}
}
Hey ebyaz,
Tried it and I see what you've done soon as I saw it I was pretty damn sure it would work but still no luck.
Now the object stays on screen but if I click the GUI button again the object doesn't disappear?
Remove the last condition in the end of you if statement "&& Soil_Survey.active == false". ebyaz forgot to remove it in his answer.
my bad, it was too long and i don't see that.$$anonymous$$ is absolutely right.
Answer by Tommynator · Mar 01, 2012 at 01:52 PM
if (GUI.Button (new Rect(Screen.width/2 - GUI2_button1_width/2 - GUI2_button1_flatspace, Screen.height/2 - GUI2_button1_height/2 - GUI2_button1_vertspace, GUI2_button1_width, GUI2_button1_height), "Soil type survey"))
{
Soil_Survey.active = !Soil_Survey.active;
}