simplifying if statements, removing active layer.

Hi, I have layers of blocks which when receiving a button press another layer of blocks is added.

here is my code:

   var layer1: GameObject;
    var layer2: GameObject;
    var layer3: GameObject;
    var layer4: GameObject;
    var layer5: GameObject;
    var btnUpTexture : Texture;
    var btnDownTexture : Texture;
    public var i : int = 1;
    
    function Start()
    {
        layer1.SetActive(true);
        layer2.SetActive(false);
        layer3.SetActive(false);
        layer4.SetActive(false);
        layer5.SetActive(false);
    }
    
    function Update()
    {
        if(i == 1)
        {
            layer1.SetActive(true);
            Debug.Log("Layer 1 Active");
            }
        if(i == 2)
        {
            layer2.SetActive(true);
            Debug.Log("Layer 2 Active");
            }
        if (i == 3)
        {
            layer3.SetActive(true);
            Debug.Log("Layer 3 Active");
            }
        if (i == 4)
        {
            layer4.SetActive(true);
            Debug.Log("Layer 4 Active");
            }
        if (i == 5)
        {
            layer5.SetActive(true);
            Debug.Log("Layer 5 Active");
            }
    }
    
    function OnGUI() {
    
        if (GUI.Button(Rect(10,10,50,50),btnUpTexture))
            {
            i++;
            }
                    Debug.Log("Clicked Up" + i);
        if (GUI.Button(Rect(10,70,50,30),btnDownTexture))
            {
            i--;
            }
                    Debug.Log("Clicked Down" + i);
    }

This works, but if I want go down a level SetActive(false) there will be a conflict as I cant have I == 5 SetActive(false) as the moment I show the layer it will remove the layer.

Should I set the value of the down button to a different value and have a if or statement? What is the best way of doing this?

Also I am aware I could probably simplify this quite abit, any ideas?

Your code is really unmaintainable, I’d do it this way:

     var layers: GameObject[]; //store all the layers in an array
     var btnUpTexture : Texture;
     var btnDownTexture : Texture;
     public var activeLayerIndex : int = 0; //use a descriptive name for the variable
     
     function Start()
     {
         ActivateLayers();
     }

     function ActivateLayers() {
         //turn on the active layer and of the rest
         for(var j : int = 0; j < layers.Length; j++) {
             layers[j].SetActive(j == activeLayerIndex);
         }
     }
     
     function OnGUI() {
     
         if (GUI.Button(Rect(10,10,50,50),btnUpTexture))
         {
             //make sure activeLayerIndex is a valid index 
             if (activeLayerIndex < layers.Length - 1) {
                 activeLayerIndex++;
             
                 //do this here (when activeLayerIndex actually changes) instead of on every update
                 ActivateLayers();
             }
         }
         if (GUI.Button(Rect(10,70,50,30),btnDownTexture))
         {
             //make sure activeLayerIndex is a valid index 
             if (activeLayerIndex > 0) {
                 activeLayerIndex--;
                 //do this here (when activeLayerIndex actually changes) instead of on every update
                 ActivateLayers();
             }
         }
     }