- Home /
Question by
zefrof · Dec 04, 2013 at 06:15 PM ·
c#guibuttonif-statements
Making a button desplay after pressing a button?
I'm trying to get a different button B to display after I press button A and then have button A disappear.
if(GUI.Button(new Rect(ScreenWidth * (0.12f),ScreenHeight * (0.78f),ScreenWidth * (0.1f), ScreenHeight * (0.2f)),"Button A")){
if(GUI.Button(new Rect(ScreenWidth * (0.025f),ScreenHeight * (0.78f),ScreenWidth * (0.12f), ScreenHeight * (0.2f)),"Button B")) {
}
Comment
Best Answer
Answer by clunk47 · Dec 04, 2013 at 06:27 PM
Use a boolean to determine whether to show either button.
#pragma strict
var showBtn1 : boolean = true;
var showBtn2 : boolean = false;
function OnGUI()
{
if(showBtn1)
{
if(GUI.Button(new Rect(Screen.width * (0.12f),Screen.height * (0.78f),Screen.width * (0.1f), Screen.height * (0.2f)),"Button A"))
{
showBtn1 = false;
showBtn2 = true;
}
}
if(showBtn2)
{
if(GUI.Button(new Rect(Screen.width * (0.025f),Screen.height * (0.78f),Screen.width * (0.12f), Screen.height * (0.2f)),"Button B"))
{
showBtn2 = false;
showBtn1 = true;
}
}
}
Answer by Matthew Scott · Dec 04, 2013 at 06:23 PM
Well, you can't make button A disappear without getting rid of button B too, when written like that.
One option would be to create booleans for each button and not to make B dependent on A E.g..
var buttonA : boolean = true;
var buttonB : boolean = false;
function OnGUI{
if(buttonA){
if(GUI.ButtonA){
buttonB = true;
buttonA = false;
}
if(buttonB){
if(GUI.ButtonB){
//Do Something
}
}
}
This is just example code...