Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GetSpectrumData() and Instantiate()

Discussion in 'Scripting' started by lushao, Nov 21, 2015.

  1. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    The script I wrote from scratch and work
    But it creates too many objects to one beat of music
    It should create one object to a single beat
    How can I do that?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enginev2 : MonoBehaviour
    5. {
    6.     //ZMIENNE
    7.     //Obiekt
    8.     public GameObject objectPrefab;
    9.     //treshold
    10.     public float spawnThreshold = 0.05f;
    11.     //Frequency
    12.     public int frequency = 0;
    13.     //Window
    14.     public FFTWindow fftWindow;
    15.  
    16.  
    17.     //ZMIENNA ILOSC SAMPLI
    18.     private float[] samples = new float[4096]; //MUST BE A POWER OF TWO
    19.  
    20.     void Start()
    21.     {
    22.  
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update ()
    27.     {
    28.         //GETSPECTRUMDATA
    29.         AudioListener.GetSpectrumData(samples, 0, fftWindow);
    30.  
    31.         //Tworzenie Obiektu
    32.         if(samples[frequency] > spawnThreshold)
    33.         {
    34.             Instantiate(objectPrefab, new Vector3(Random.Range(-10.0f, 10.0f), 5, 0), Quaternion.identity);
    35.         }
    36.     }
    37. }
    38.  
    @StarManta
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    you could do something like this.
    Code (CSharp):
    1.  
    2. GameObject clone;
    3.  
    4.  if(samples[frequency] > spawnThreshold  && clone == null)
    5.         {
    6.             clone = (GameObject)Instantiate(objectPrefab, new Vector3(Random.Range(-10.0f, 10.0f), 5, 0), Quaternion.identity);
    7.         }
    8. else
    9.     clone = null;
     
  3. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    Not working for me ;c
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Example : MonoBehaviour {
    5.  
    6.  
    7.     //ZMIENNE
    8.     //Obiekt
    9.     public GameObject objectPrefab;
    10.     //treshold
    11.     public float spawnThreshold = 0.05f;
    12.     //Frequency
    13.     public int frequency;
    14.     //Window
    15.     public FFTWindow fftWindow;
    16.  
    17.     GameObject clone;
    18.    
    19.     //ZMIENNA ILOSC SAMPLI
    20.     private float[] samples = new float[4096]; //MUST BE A POWER OF TWO
    21.    
    22.     void Start()
    23.     {
    24.        
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update ()
    29.     {
    30.         //GETSPECTRUMDATA
    31.         AudioListener.GetSpectrumData(samples, 0, fftWindow);
    32.         //Tworzenie Obiektu
    33.         if(samples[frequency] > spawnThreshold)// && clone != null)
    34.         {
    35.             clone = (GameObject)Instantiate(objectPrefab, new Vector3(Random.Range(-10.0f, 10.0f), 5, 0), Quaternion.identity);
    36.         }
    37.         else
    38.             clone = null;
    39.     }
    40.  
    41.  
    42. }
    43.  
    make sure your objectPrefab is valid. this error is not a code error is an error concerning the object you want to instantiate
     
    Last edited: Nov 21, 2015
  5. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    Everything its ok. Prefab looks good.
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    the code i posted above is tested and worked fine for me. if you get the error: the thing you want to instantiate is null. usually means your prefab is broken.
    or do you mean everything is ok as in it all works fine now?
     
    Last edited: Nov 22, 2015