- Home /
Spawn help, 4 spawners but need one object on screen at a time
I have a 2D game that I am building and I need help with my spawners. I have a spawner to the left, right, top, and bottom of the camera that spawn objects that go to an object in the middle of the screen that rotates to catch them. I have the spawners set up to spawn the object at a random time but objects often reach the middle at the same time making it impossible to catch them both. I have this one code on all 4 spawners. I need a way to merge the spawning of all 4 or a box that checks if an object is already on screen before it spawns one or not. All my code is in C# and this is my first project.
using UnityEngine; using System.Collections;
public class RandomTop : MonoBehaviour {
bool isSpawning = false;
public float minTime;
public float maxTime;
public GameObject[] prefabs;
IEnumerator SpawnObject(int index, float seconds)
{
yield return new WaitForSeconds(seconds);
Instantiate(prefabs[index], transform.position, transform.rotation);
isSpawning = false;
}
void Update ()
{
if(! isSpawning)
{
isSpawning = true;
int enemyIndex = Random.Range(0, prefabs.Length);
StartCoroutine(SpawnObject(enemyIndex, Random.Range(minTime, maxTime)));
}
}
}
Answer by tebandesade · Jul 02, 2014 at 09:00 PM
You might want to check out"Choosing from a Set of Items Without Repetition" of the manual http://docs.unity3d.com/Manual/RandomNumbers.html