Make an int switch?

Im making a menu system and every time the player presses escape, i want the an int called currentPanel to go down, so it knows when the menu is closed. however, when the menu is at -1, i want it to open the first panel rather than closing it.

so heres what would happen if you had the 4th menu panel open and pressed esc 5 times:

  1. move to 3.
  2. move to 2.
  3. move to 1.
  4. move to 0.
  5. close menu.
  6. open menu 0.

i tried

if (Input.GetKeyDown (KeyCode.Escape))
		{
			if (CurrentPanel < 0)
			{
				CurrentPanel ++;
			}
			if (CurrentPanel == 0)
			{
				CurrentPanel --;
			}

but it doesnt work because when one switches, the other switches.

Just need an else before the if as below. Otherwise, both statements will execute, and you’re back at -1.

    if (Input.GetKeyDown (KeyCode.Escape))
    {
        if (CurrentPanel < 0)
        {
            CurrentPanel ++;
        }
        else if (CurrentPanel == 0)
        {
            CurrentPanel --;
        }
    }

A switch like you’re asking for would be:

switch (CurrentPanel)
{
    case -1:
        CurrentPanel++;
        break;

    case 0:
        CurrentPanel--;
        break;

    // and so on
}

For a more robust menu system I would implement something like the following:

public enum Menu
{
    Main,
    Options,
    NewGame,
    Etc
}

private Stack<Menu> m_MenuStack;

public void GoToMenu(Menu menu)
{
    m_MenuStack.Push(menu);
}

public void GoToPreviousMenu()
{
    m_MenuStack.Pop();
    if (m_MenuStack.Count == 0)
        GoToMenu(Menu.Main);
}

public void Update()
{
    if (Input.GetKeyDown(KeyCode.Escape))
        GoToPreviousMenu();
}

public void OnGUI()
{
    Menu current = m_MenuStack.Peek();

    switch (current)
    {
        case Menu.Main:
            // Draw the main menu
            break;

        case Menu.Options:
            // Draw the options menu
            break;

        // etc

        default:
            throw new System.ArgumentOutOfBoundsException();
    }
}