How to switch between two toggles?

Hi,

For my GUI, I’ve made 2 toggle buttons to open a menu. Here’s a short version of code:

 var showmenu1 : boolean = false;
 var showmenu2 : boolean = false;

 function OnGUI()
 {
  showmenu1 = GUI.toggle(Rect...);
  showmenu2 = GUI.toggle(Rect...);

  GUILayout.BeginArea(Rect..);
  if(showmenu1)
  {
   //showmenu1 buttons
  }
 
  if(showmenu2)
  {
   //showmenu2 buttons
  }
  GUILayout.EndArea();
  
 }

What I’m trying to achieve is:

 if(showmenu1)
 {
  showmenu = true 
  showmenu2 = false;
 }

 if(showmenu2)
 {
  showmenu = false;
  showmenu2 = true;
 }

The problem I’m having right now is the order the showmenus are written. When I open menu1 and want to open menu2, it will hide the menu1 but it doesn’t work if I try to open menu1 from menu2. Wonder if anyone has seen this before. Even more, what would be the best solution/approach to this.

Thanks in advance.
-Hakimo

with only 2 menus, 1 toggle and a bool is enough.

if true → show menu 1
else → show menu 2

Hi Dreamora,

The reason I made two toggles is that I want them to be separated. When the application starts, the menus are hidden by default and you can only see two small toggle buttons; one top-left and the other top-right.

Only if I want to see it, then I’ll click on a toggle. You can basically open one menu at a time but if I want to be able to open one menu and open the other. I hope that makes sense.

Thanks again.
-Hakimo

in that case have a !showMenu1 in front of GUI.toggle for showMenu2. that way only one of the two can be active at a time.

Thanks again for the reply. Sorry but what do you mean by "showMenu1 in front of GUI.toggle?

Cheers.
-Hakimo

showmenu2 = GUI.toggle(Rect...);

becomes

showmenu2 = GUI.toggle(Rect...)  !showmenu1;

I put it afterwards there so the goggle is visible independent on showmenu1 selected

var toggleBoolLeft : boolean = true;
var toggleBoolRight : boolean = false;

function OnGUI () {
	toggleBoolLeft = GUILayout.Toggle (toggleBoolLeft, "Toggle")  !toggleBoolRight;
	toggleBoolRight = GUILayout.Toggle (toggleBoolRight, "Toggle")  !toggleBoolLeft;
}

Effects : only one toggle can be active at the same time ; trying to activate the non active results in nothing achieved : neither the toggle is active nor the activated toggle is deactivated. Nice try anyway.

I had worked on this problem previously but I gave up at this time. I will think about it again a little harder…

Gutted. Why is it like that? Is it programming logic that’s wrong or is it OnGUI?

I guess the solution that I can try is separate them into two game objects and set the .active = false. I’ll try it and see how it goes.

Thanks again.
-Hakimo

It seems you’re over-complicating the task a bit. If I understand correctly, you want the following three possible states:

  • Nothing shown: Clicking a toggle enables its menu.
  • A shown: Clicking the toggle for A disables A, clicking the toggle for B disables A and enables B.
  • B shown: Clicking the toggle for B disables B, clicking the toggle for A disables B and enables A.

This should do just that (untested):

bool showA = false, showB = false;

void OnGUI ()
{
    if (GUILayout.Toggle (showA, "A"))
    {
        MenuA ();
        showB = !showA = true;
    }
    else
    {
        showA = false;
    }

    if (GUILayout.Toggle (showB, "B"))
    {
        MenuB ();
        showA = !showB = true;
    }
    else
    {
        showB = false;
    }
}

void MenuA ()
{
    GUILayout.Label ("A");
}

void MenuB ()
{
    GUILayout.Label ("B");
}