How to solve "Array Index is out of range"

I’m developing a project on Unity where I want to make an app that connects Neurosky’s Mindwave Mobile with Sphero. Essentially Mindwave Mobile sends brainwave signalls to the app as input and the app sends as output information for the Sphero robot to move.

However, Neurosky helps by making avaiable an example of a project using Mindwave Mobile to work on. In the example theres a demo and the demo, when I try to run it, it keeps getting me this error:

IndexOutOfRangeException: Array index is out of range.
DisplayData.OnGUI () (at Assets/NeuroSkyAssets/NeuroSkyScripts/DisplayData.cs:125)

Which is associated with this code line:

line 6: public Texture2D[] signalIcons;
line 8: private float indexSignalIcons = 1;
line 52: void OnUpdatePoorSignal(int value){
		PoorSignal = value;
		if(value == 0){
      		indexSignalIcons = 0;
			enableAnimation = false;
		}else if(value == 200){
      		indexSignalIcons = 1;
			enableAnimation = false;
		}else if(!enableAnimation){
			indexSignalIcons = 2;
			enableAnimation = true;
line 64:	}

line 111: void FixedUpdate(){
			if(indexSignalIcons >= 4.8){
				indexSignalIcons = 2;
			indexSignalIcons += animationInterval;
line 119:	} 

line 121: void OnGUI(){
		GUILayout.Label("Demo App");
line 125:GUILayout.Label(signalIcons[(int)indexSignalIcons]);

This is every code apart I’m guessing that is associated with the error. What should I do?

How large is the signalIcons array? Have you assigned textures to it? How many?

The code you show is deciding which texture to display based on various things. It calculates and stores the value using indexSignalIcons which is used as an index into the signalIcons array. The value it’s calculating is either negative (which doesn’t appear to be the case) or it’s larger than the number of textures stored in the array.

To fix this, you need to either add all of the expected icons to the array, or figure out why the calculated index isn’t calculating properly. To help with the latter you can check the index before using it and log a message with the value if it’s out of range. In OnGUI, before line 125…

if (indexSignalIcons < 0 || indexSignalIcons >= signalIcons.Length)
  Debug.Log("Signal Icon Index is out of range: " + ((int)indexSignalIcons));