How do I delay instanciated objects randomly to ensure they don't spawn at the same time? (C#)

Hello, I am making a Rhythm game within Unity; and in this, I have 3 spawners, each spawning beats which can be removed once they enter a trigger and the player presses a button. However, now I have them actually spawning to music I have bizarre issues where all 3 spawners either A) Spawn at the same time, and stop spawning at all 30 seconds or so in, and B) Spawn far too many objects at once, causing blockages and what not, balls spiralling all over the screen.

So my question to this is A) How would I go about delaying these spawners randomly, or B) Is there a better fix for this than that?

Note; I do have a random delay but it doesn’t work in this version, as changing the values will only cause it to spawn far too many spheres/beats.

using UnityEngine;
using System.Collections;


public class beat : MonoBehaviour {
	
	public Transform target;
	public int minRange;
	public bool follow;
	private float speed;
	public Rigidbody projectile;
	private float cooldown = 0.5f;
	public int detail = 500;
	public float amplitude = 0.1f;
	public float minValue = 1.0f;


	
	void Start () {

	}
	
	

	
	
	void Update(){
		cooldown -= Time.deltaTime;
		float[] info = new float[detail];
		AudioListener.GetOutputData(info, 0); 
		float packagedData = 0.0f;
		
		for(int x = 0; x < info.Length; x++)
		{
			packagedData += System.Math.Abs(info[x]);   
		}
		
		if (packagedData/info.Length > 0.5f) {
			cooldown += Random.Range(1,3);
			Rigidbody clone;
			clone = Instantiate (projectile, transform.position, transform.rotation) as Rigidbody; //This seems to create the clone
			clone.velocity = transform.TransformDirection(Vector3.forward * 40); //This details the velocity of the projectile

		
			}
			
		}

			
		}

Cheers!

Cool stuff! I haven’t worked on a rhythm game before, but I think I can see how this is working. Let’s break down the if statement that controls whether an object is spawned or not:

if (packagedData/info.Length > 0.5f) {...}

The way your class is set up, the length of the info array, (info.Length) is always going to be the same. You instantiate this array in your update() method as info = new float[detail];, effectively giving it a size of detail = 500 from the field in your class.

packagedData comes from the summation of the absolute value of the info array. I can only guess then, that you’re summing the magnitude of each frame, dividing by the length, and getting an average amplitude of your sound for 500 “frames” where those frames are determined by whatever the data rate is of the AudioListener (not sure how the data collection works). But if each 500 frames of your sound have an average above 0.5, I can see how you’d start spawning like crazy, and conversely, how lulls would create empty space.

Without knowing what the specific goals of your beat-spawning logic are, I don’t know that I can answer your question fully. However, I can say that if your issue is just that you’re spawning too often, I’d add in that cooldown variable to your if statement. Something like:

if (packagedData/info.Length > 0.5f && cooldown < 0)

This should effectively limit the number of times you can spawn beats in a row.