problem when refering to a bool array c#

Hello everyone,

I’m trying to refer to a certain bool array and I’m getting the error : IndexOutOfRangeException: Array index is out of range. What’s wrong with it?

using UnityEngine;
    using System.Collections;

    public class Level : MonoBehaviour 
    {

        public GameObject[] blocksPrefabs;
        public GameObject[] comboPrefabs;
        public Transform defaultSpawn;
        public Transform comboSpawn;
        public float startDelay=0;
        public float intervals=0;
        public bool[] comboCheck;
       
        //false should be shapes, true should be colors
        bool mode = false;
        float successRatio = 0.0f;
        float gameStart = 0.0f;
        float range = 2.6f;
        [HideInInspector]
        public float goodPitch = 0.6f;
        [HideInInspector]
        public int pitchStacks = 0;
        [HideInInspector]
        public int Combo = 0;
        [HideInInspector]
        public int totalLanded = 0;
        [HideInInspector]
        public int totalSuccess=0;


        void Start ()
        {

            InvokeRepeating ("InstantiateBlock", startDelay, intervals);
        }

        void InstantiateBlock ()
        {
            Vector3 Spawn = new Vector3 (Random.Range (-range, range), 6f, 0);
            GameObject Shapes = Instantiate(blocksPrefabs[Random.Range (0,blocksPrefabs.Length)], Spawn, defaultSpawn.rotation) as GameObject;

        }
        void Update ()
        {
            if (totalLanded >= 1) {
                successRatio = ((float)totalSuccess / (float)totalLanded)*100;
                Debug.Log (successRatio);
            }

            if (Combo >= 5 && comboCheck[0] == false) {

                comboCheck[0]=true;
                GameObject Combos = Instantiate(comboPrefabs[0], comboSpawn.position, comboSpawn.rotation) as GameObject;
            }
[code]

are you setting the comboCheck array to anything? otherwise by default it’ll be empty

Are you sure it’s the bool array causing the problem? If you know you have 1 element in your bool array then it shouldn’t cause an issue… however this line is wrong:

 GameObject Shapes = Instantiate(blocksPrefabs[Random.Range (0,blocksPrefabs.Length)], Spawn, defaultSpawn.rotation) as GameObject;

That line will intermittently throw an IndexOutOfRangeException because you are using blocksPrefabs.Length. Instead it should be blocksPrefabs.Length - 1 because arrays are zero based index. So the length of your array might be “5” but the uppermost index is actually “4”, so if that random function actually picks “5” then it’s going to throw an exception.