IE numerator to spawn falling objects along x axis c#

Hey guys,

I found this bit script on another post and I tried to use it. The script compiles just fine but when I test the thing, my object does not spawn. I randomly decided to add the script to the camera and the object shows up but it spawns so fast it looks like a fountain and nearly crashes the computer. What’s the deal? I’d like for the object to spawn randomly along the x axis every 5 - 10 seconds. Thanks in advance, you guys are great. I am still learning the basics.

using UnityEngine;
using System.Collections;

public class ObjectSpawn : MonoBehaviour {

public float minTime = 5f; 
public float maxTime = 10f;
public float minX = -5.5f;
public float maxX = 5.5f;
public float topY = 5.5f;
public float z = 0.0f;
public int count = 200;
public GameObject prefab;

public bool doSpawn = true;

void Start() {
	StartCoroutine(Spawner());
}

IEnumerator Spawner() {
	while (doSpawn && count > 0) {
		Vector3 v = new Vector3(Random.Range (minX, maxX), topY, z);
		Instantiate(prefab, v, Random.rotation);
		count--;
		yield return new WaitForSeconds(Random.Range(minTime, maxTime));
	}
}

}

I just ran a quick test of your script, and it runs exactly as you want it to and as indicated by the default values. As a test, start a new scene, add an empty game object, attach this script to the empty game object, initialize ‘prefab’ through drag and drop.

Two things to look at in your original code: 1) make sure you prefabs don’t have this script, and 2) select the game object with this script and look at the values in the Inspector. The initialization of variables you have at the of the file are only used when the script is attached to the object. At all other times, the values are taken from the Inspector settings.