Trying to turn items on/off with a function instead of using Update

So I have code that turns items on/off for different options. When I tried to add a routine so I could make my code nicer I got an error saying the array was out of range. It’s commented out at the end. Not sure what’s up.

using Pixyz.Utils;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputTest : MonoBehaviour
{
    public int aIndex;

    public GameObject[] Collection1;
    public GameObject[] Collection2;
    public GameObject[] Collection3;

    private bool blActive;
    private int intCount;
    private bool blOpt1;
    private bool blOpt2;
    private bool blOpt3;

    private string strTemp;
    private int i;
    // Start is called before the first frame update
    void Start()
    {
        intCount = 0;
    }

    // Update is called once per frame
    void Update()
    {

        if (OVRInput.GetDown(OVRInput.Button.One))
        {
            //Set booleans to determine configuration for different options
            blOpt1 = false;
            blOpt2 = false;
            blOpt3 = false;

            if (intCount == 1)
            {
                blOpt1 = true;
                blOpt2 = false;
                blOpt3 = false;
                intCount = intCount + 1;
            }
            else if (intCount==2)
            {
                blOpt1 = false;
                blOpt2 = true;
                blOpt3 = false;
                intCount = intCount + 1;
            }
            else if (intCount==3)
            {
                blOpt1 = false;
                blOpt2 = false;
                blOpt3 = true;
                intCount = 0;
            } 
            else
            {
            intCount = intCount + 1;
            }
           
            //Set options based on bools           
            for (int i = 0; i < Collection1.Length; i++)
            {
                Collection1[i].SetActive(blOpt1);
            }
            
            for (int i =0; i < Collection2.Length; i++)
            {
                Collection2[i].SetActive(blOpt2);
            }

            for (int i =0; i < Collection3.Length; i++)
            {
                Collection3[i].SetActive(blOpt3);
            }
           


            //TurnObjectsOff(Collection1);

        }
    }

    //void TurnObjectsOff(GameObject[] objects)
    //{

    //    for ( i = 0; i < objects.Length; i++)
    //            blActive =! objects[i].activeSelf;
    //            objects[i].SetActive(blActive);

    //}


}

Just that you forgot brackets, so only line 92 was part of the for loop, and line 93 ran after the for loop was done (and ‘i’ was indeed out of range of the array).

void TurnObjectsOff(GameObject[] objects)
{
  for ( i = 0; i < objects.Length; i++) {
    blActive =! objects[i].activeSelf;
    objects[i].SetActive(blActive);
  }

}

FYI it’s better to just declare the int i as part of the for loop like you are doing for most of the for loops in your code, instead of making an instance-wide variable for the loop like you are doing in this function.

AAAHHHHH, DUH! Thanks.