GetSpectrumData, GetOutputData

I’m relatively new to the Unity. I’m making a simple visualizer generating circle of blocks, and I’m passing the pectral values to the height of the blocks in a cycle. I’m making it with various tutorials. Sadly the code they are using is already obsolete. And I cannot for the life of me figure out how to correct it. On the Official Unity document site, instead of proper example of valid code, I get only this error message : GetSpectrumData returning a float is deprecated, use GetOutputData and pass a pre allocated array instead. Can anyone rewrite my code, so I can understand where is the mistake? It’s on line 16.

Here is my Code:

using UnityEngine;
using System.Collections;

public class Spetrum : MonoBehaviour {

// Use this for initialization
public GameObject prefab;
public int numberOfObjects = 20;
public float radius = 5f;
public GameObject[] cubes;

void Start() {
	for (int i = 0; i < numberOfObjects; i++) {
		float angle = i * Mathf.PI * 2 / numberOfObjects;
		Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
		Instantiate(prefab, pos, Quaternion.identity);
	}
	cubes = GameObject.FindGameObjectsWithTag ("cubes");
}
void Update () {
	float[] spectrum = AudioListener.GetSpectrumData (1024, 0, FFTWindow.Hamming);
	for (int i = 0; i < numberOfObjects; i++) {
		Vector3 previousScale = cubes*.transform.localScale;*

previousScale.y = spectrum * 20;
* }*
* }*

}

Hi, I know this is old question, but I am following the same tutorial and I found a solution. I’m writing this in case you still haven’t figured it out. The solution is simple. You have to do this:

Replace:

float spectrum = AudioListener.GetSpectrumData (1024, 0, FFTWindow.Hamming);

With:

float samples = new float[1024];
AudioListener.GetSpectrumData (samples, 0, FFTWindow.Hamming);

For some reason they decided this is a better way and they depricated the overloaded method with a return value. That’s all there is to it :slight_smile: