Easy way to disable all panels except for the current one in Unity

Stick them in an array, iterate on the array. So on your button that opens a panel, send an index value that corresponds to the panel to be opened. Then you can just turn on that panel from the array. Something like this:

[SerializeField] GameObject[] panelList;

void ResetPanels()
{
    for(int i = 0; i < panelList.Length; i++)
    {
        if(panelList[i].activeSelf) panelList[i].SetActive(false);
    }
}

public void OpenPanel(int index)
{
    ResetPanels();
    panelList[index].SetActive(true);
}


Comments