I'm trying to build a 3D Spectograph and get a bunch of frequency data for a given point in time. I need to simplify this in order to be able to handle the instantiating of a lot of prefabs. So I use a fixed count of Rows of fequency data to look at:
int spectrumRows = 24;
I then call this function to "simplify" the detail of the frequency spectrum by adding values together and then just adding their median to a float array called "floatArray". This then is passed to another float array, "simpleSpectrum", which tells me that there is this reference error.
public void addToSimpleSpectrum(float[] spectrum) {
float spectralsInRow = spectrum.Length / spectrumRows;
Math.Round(spectralsInRow);
float[] floatArray = new float[(int)spectralsInRow];
for(int i = 0; i < spectralsInRow; i++){
float median = 0f;
float sum = 0f;
for (float o = spectralsInRow*i; o < (spectralsInRow*(i+1)); o++) {
sum = sum + spectrum[(int)o];
}
median = sum / spectralsInRow;
floatArray[i] = median;
}
simpleSpectrum.Add(floatArray);
}
The error I'm getting in Unity is:
System.NullReferenceException: Object reference not set to an instance of an object at [...]:46
Which means that the "simpleSpectrum.Add(floatArray);" doesn't work because it doesn't know what the floatArray is. The "public List<float[]> simpleSpectrum" is created in global scope. But the floatArray should be accessible within the function, or not?
I hope somebody can help me. C# is new to me!