When increasing objects instantiated, wierd error follows

Most errors tell me where I went wrong, but this one confuses me. The error at hand is this:

IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
SoundDisplay.Bands () (at Assets/SoundDisplay.cs:106)
SoundDisplay.NumberOfBands (System.String number) (at Assets/SoundDisplay.cs:156)
UnityEngine.Events.InvokableCall1[System.String].Invoke (System.Object[ ] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:189) UnityEngine.Events.InvokableCallList.Invoke (System.Object[ ] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637) UnityEngine.Events.UnityEventBase.Invoke (System.Object[ ] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773) UnityEngine.Events.UnityEvent1[T0].Invoke (.T0 arg0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:53)
UnityEngine.UI.InputField.SendOnSubmit () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:1544)
UnityEngine.UI.InputField.DeactivateInputField () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:2264)
UnityEngine.UI.InputField.OnDisable () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:544)

This is all foreign to me but it seems like a Unity Engine error, but I still think that my code is the reason it’s getting fired off to me. The code it’s referencing looks like this:

Start function initiates this:

void Bands() {
        settingsReference.barsEnabled = false;
        foreach (GameObject band in bands) {
            Destroy(band);
        }
        bandOne.GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.width / numberOfBands, 1);
        bands[0] = bandOne;
        for (int i = 1; i < numberOfBands + 1; i++) {
            bands[i - 1] = Instantiate(bandOne, new Vector3((Screen.width / numberOfBands) * i, 0, 0), new Quaternion(0, 0, 0, 0), GameObject.Find("Canvas").transform);
        }
        settingsReference.barsEnabled = true;
    }

then the bands are updated to show audio every frame:

void BandsUpdate() {
        for (int i = 0; i < numberOfBands; i++) {
            bands[i].GetComponent<RectTransform>().sizeDelta = new Vector2(bands[i].GetComponent<RectTransform>().sizeDelta.x, samples[i] * (barChangeFrequency * 10));
            if (bands[i].GetComponent<RectTransform>().sizeDelta.y <= 2) {
                bands[i].GetComponent<RectTransform>().sizeDelta = new Vector2(bands[i].GetComponent<RectTransform>().sizeDelta.x, 0);
            }
            if (bands[i].GetComponent<RectTransform>().sizeDelta.y > maxBarHeight) {
                bands[i].GetComponent<RectTransform>().sizeDelta = new Vector2(bands[i].GetComponent<RectTransform>().sizeDelta.x, maxBarHeight);
            }
        }
    }

and then later, the user can change the number of bands drawn on the screen

public bool PowerOfTwo(int x) {
        return (x != 0) && ((x & (x - 1)) == 0);
    }
   
    public void NumberOfBands(string number) {
        if (PowerOfTwo(int.Parse(number))) {
            numberOfBands = int.Parse(number);
            Bands();
        }
    }

Once the user changes the number of bands to anything higher than 128 (i.e. 256, 512, so on) that strange error is thrown. Can anyone tell me why? Obviously something is out of range, but what? Can the engine not work fast enough to update all the new bands before the Update is called, or is it something else? Let me know, Unity pros. Thanks guys!

Based on the error, you’ll want to look at SoundDisplay.cs . The error is saying that there is an Array in this script that you are referencing, and the index you’re looking up doesn’t exist. Arrays are sized to a specific size. If you try and access something outside of that sized array, you get the error you see.

So for example, say you had an array of ints called stuff. you sized it to 6. It may look like this:

public int[ ] stuff = new int[6]; // indexes of the array are 0,1,2,3,4,5.

Now later on you might access the array in a loop, say with something like this:

for (int i=0; i < stuff.Length; i++)
{
   do_something(stuff[i]);
}

That would iterate through the loop 6 times (index of 0 to 5), and access the array perfectly (assuming my quickly slapped together code is correct). However, you might incorrectly do something like this:

for (int i=0; i <= stuff.Length; i++)
{
   do_something(stuff[i]);
}

That would resuly in 7 loops through, accessing indexes 0 through 6. You’d then see the error above when you tried to basically do this:

do_something(stuff[6]);

There is no element in the array for an index of 6, because arrays start at 0, so the last index would be whatever the size is, minus 1 (5 in my example). So that’s kind of the gist of why you see those errors. You might not be looping through in the way I described, but something is accessing this array that triggered the error outside the bounds of the array. Feel free to post the script I mentioned above if you need further help. But try to paste the whole thing so the line numbers match up.

My guess is somewhere the array size needs to be adjusted. But it’s hard to know without seeing the full script.

1 Like

That appears to have worked, I changed the way the for loops are interacting with the arrays and now it doesn’t return that error. Thank you

Great, glad to help, and glad you were able to figure it out on your own!