Simple GUI question

If I write:

function onGUI(){
  if (condition){
    if (GUI.Button(Rect(x1,y1,h,w),"this")) doThis();
  }else{
    if (GUI.Button(Rect(x2,y2,h,w),"that")) doThat();
  }
}

The “this” button appears when “condition” is true, else, the “that” button appears. But when I compile it as a Windows .exe or a UnityPlayer, both buttons appear.

Did I miss something?

What you describe is not possible. That means if you’re seeing two buttons the only possibilities are:

  • There’s another script attached somewhere, maybe on a different object, which might shows the other button. Keep in mind that if this script has two instances each instance has it’s own condition if it’s based on a member variable. Since you only showed pseudo code we can’t investigate.

Your script alone can only show 1 button at a time.

Another possible but not very likely situation could be that your condition is switching state every frame so you get a quick flicker of both buttons. If you have a high framerate you might not notice the flicker. However in this case it’s unlikely that the buttons actualy respond to clicks.

edit
Here, i just took your script line by line (fixing the OnGUI and adding some debug controls):

Works pretty well on my machine.

puts “thas” button inside of condition “this”

function onGUI(){
  if (condition){
    if (GUI.Button(Rect(x1,y1,h,w),"this")) doThis(){
     if (GUI.Button(Rect(x2,y2,h,w),"that")) doThat();
    }
  }
}