Question by
palavence · May 14, 2017 at 11:45 AM ·
arraysfloatindexoutofrangeexception
IndexOutOfRangeException: Array index is out of range. on "frequency = frequencies[thisFreq];"
Hey guys, I've been following tutorials to make a Synthesizer in Unity. All fine until I reach this point: Basically I want to create an Array of frequencies and, whenever (while the game is playing) I press the Space bar button my sound jumps to the next frequency. This is just for a testing phase and I'd really like to solve this, but can't find a way to do it. When I do "frequency = frequencies[thisFreq];" the compiler doesnt like it. Says there's an Index out of range error.
public float volume = 0.1f;
public float[] frequencies;
public int thisFreq = 0;
void start()
{
frequencies = new float[8];
frequencies[0] = 440;
frequencies[1] = 494;
frequencies[2] = 523;
frequencies[3] = 587;
frequencies[4] = 659;
frequencies[5] = 740;
frequencies[6] = 831;
frequencies[7] = 880;
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
gain = volume;
if (thisFreq > 7)
{
thisFreq = 0;
}
frequency = frequencies[thisFreq];
thisFreq = thisFreq + 1;
}
Comment