Question by
YngveL · Nov 08, 2016 at 01:09 PM ·
arrayerror-messageaudiolistener
Array index out of range on a Audio Visualizer
I have a "working" audio visualizer running, the white bars are jumping up and down to the music. But I'm getting constant Array index errors. Can anyone take a look at my code and try to help me out?
using UnityEngine;
using System.Collections;
public class Spectrum : MonoBehaviour
{
public GameObject prefab;
public float gridX = 5f;
public float gridY = 5f;
public float spacing = 2f;
public int numberOfObjects = 20;
public GameObject[] squares;
void Start()
{
for (int y = 0; y < gridY; y++)
{
for (int x = -5; x < gridX; x++)
{
Vector3 pos = new Vector3(x, 0, y) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
}
}
squares = GameObject.FindGameObjectsWithTag("Square");
}
void Update()
{
float[] spectrum = AudioListener.GetSpectrumData(1024, 0, FFTWindow.Hamming);
for (int i = 0; 1 < numberOfObjects; i++)
{
Vector3 previousScale = squares[i].transform.localScale;
previousScale.y = Mathf.Lerp(previousScale.y, spectrum[i] * 40, Time.deltaTime * 30);
squares[i].transform.localScale = previousScale;
}
}
}
array-index-error.png
(18.1 kB)
Comment
Best Answer
Answer by doublemax · Nov 08, 2016 at 01:17 PM
for (int i = 0; 1 < numberOfObjects; i++)
Maybe you're using a font where 1 (one) and i (letter) look similar?
for (int i = 0; i < numberOfObjects; i++)
Oh wow. Can't believe I missed that... Now I feel stupid. Thank you very much!
Your answer
Follow this Question
Related Questions
Using arrays of vector3s to avoid new 1 Answer
Texture2D to Plane: Array Index is Out of Range 1 Answer
Unknown Argument Out of Range Index Error On Card Game 1 Answer
Adding GameObjects to an Array...Problems. 0 Answers
.Contains(array) 1 Answer