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.UnityEvent
1[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!