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

Instantiate creates too many objects

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

  1. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    To GetSpectrumData () create objects using Instantiate().
    The problem is that formed after 5-10 objects, rather than 1.

    They had to create one object to a single bit of of music.

    How do I do this?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Engine : MonoBehaviour
    5. {
    6.     //ZMIENNE
    7.     //Obiekt
    8.     public GameObject objectPrefab;
    9.     //treshold
    10.     public float spawnThreshold = 0.05f;
    11.     //Frequency
    12.     public int frequency = 30;
    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.  
    32.  
    33.        
    34.         if (samples[frequency] > spawnThreshold)
    35.         {
    36.             Instantiate(objectPrefab, new Vector3(Random.Range(-10.0f, 10.0f), 5, 0), Quaternion.identity);
    37.  
    38.         }
    39.        
    40.          
    41.      
    42.        
    43.        
    44.  
    45.     }
    46.  
    47. }
    48.  
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    just check if it is already spawned or not?
    Code (CSharp):
    1. GameObject clone;
    2.  
    3. void Update()
    4. {
    5.     if(clone != null)
    6.         return;
    7.     else
    8.         clone = (GameObject) Instantiate(objectPrefab, new Vector3(Random.Range(-10.0f, 10.0f), 5, 0), Quaternion.identity);
    9.  
    10. }
     
  3. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    hmm so (just as in your previous thread), the principal is simple:
    If you only want it ever to create one object under certain conditions, you make a variable where you can "put" the instantiated object "in". as i did above with:
    Code (CSharp):
    1. clone = (GameObject) Instantiate(objectPrefab, new Vector3(Random.Range(-10.0f, 10.0f), 5, 0), Quaternion.identity);
    so now you can check if there already is an object in there before you instantiate one.
    Code (CSharp):
    1. if(clone != null)
    2.         return;
    and so this will only ever create one object for you as long as you don't clear it.
    Now if you want to create an object every time the condition is met. it means you first need to set the clone reference back to zero as i did before in your other thread.

    before you said it has to create one every beat so my guess is you need the other script i showed you... the "the thing you want to instantiate is null error" is a problem with a faulty prefab. remake your prefab and try again.
     
  5. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    I remaking prefab 20x
    Still error is the same...
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Here's a working version
     

    Attached Files:

  7. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    Thanks for package.