Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Editor & General Support' started by JBEDUnity, Aug 5, 2020.

  1. JBEDUnity

    JBEDUnity

    Joined:
    Jul 13, 2020
    Posts:
    23
    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.

    Code (CSharp):
    1. using Pixyz.Utils;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class InputTest : MonoBehaviour
    7. {
    8.     public int aIndex;
    9.  
    10.     public GameObject[] Collection1;
    11.     public GameObject[] Collection2;
    12.     public GameObject[] Collection3;
    13.  
    14.     private bool blActive;
    15.     private int intCount;
    16.     private bool blOpt1;
    17.     private bool blOpt2;
    18.     private bool blOpt3;
    19.  
    20.     private string strTemp;
    21.     private int i;
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         intCount = 0;
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.  
    32.         if (OVRInput.GetDown(OVRInput.Button.One))
    33.         {
    34.             //Set booleans to determine configuration for different options
    35.             blOpt1 = false;
    36.             blOpt2 = false;
    37.             blOpt3 = false;
    38.  
    39.             if (intCount == 1)
    40.             {
    41.                 blOpt1 = true;
    42.                 blOpt2 = false;
    43.                 blOpt3 = false;
    44.                 intCount = intCount + 1;
    45.             }
    46.             else if (intCount==2)
    47.             {
    48.                 blOpt1 = false;
    49.                 blOpt2 = true;
    50.                 blOpt3 = false;
    51.                 intCount = intCount + 1;
    52.             }
    53.             else if (intCount==3)
    54.             {
    55.                 blOpt1 = false;
    56.                 blOpt2 = false;
    57.                 blOpt3 = true;
    58.                 intCount = 0;
    59.             }
    60.             else
    61.             {
    62.             intCount = intCount + 1;
    63.             }
    64.            
    65.             //Set options based on bools          
    66.             for (int i = 0; i < Collection1.Length; i++)
    67.             {
    68.                 Collection1[i].SetActive(blOpt1);
    69.             }
    70.            
    71.             for (int i =0; i < Collection2.Length; i++)
    72.             {
    73.                 Collection2[i].SetActive(blOpt2);
    74.             }
    75.  
    76.             for (int i =0; i < Collection3.Length; i++)
    77.             {
    78.                 Collection3[i].SetActive(blOpt3);
    79.             }
    80.            
    81.  
    82.  
    83.             //TurnObjectsOff(Collection1);
    84.  
    85.         }
    86.     }
    87.  
    88.     //void TurnObjectsOff(GameObject[] objects)
    89.     //{
    90.  
    91.     //    for ( i = 0; i < objects.Length; i++)
    92.     //            blActive =! objects[i].activeSelf;
    93.     //            objects[i].SetActive(blActive);
    94.  
    95.     //}
    96.  
    97.  
    98. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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).

    Code (CSharp):
    1. void TurnObjectsOff(GameObject[] objects)
    2. {
    3.   for ( i = 0; i < objects.Length; i++) {
    4.     blActive =! objects[i].activeSelf;
    5.     objects[i].SetActive(blActive);
    6.   }
    7.  
    8. }
    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.
     
  3. JBEDUnity

    JBEDUnity

    Joined:
    Jul 13, 2020
    Posts:
    23
    AAAHHHHH, DUH! Thanks.