- Home /
Instantiate over time?
How can I spawn objects over time and how can I randomize the number of objects within a set range, and how can I randomize the object type (ex: at the end of every 30 second interval spawn anywhere from 3-8 objects with the possibility of object A, Object B or Object C). Thank you.
Answer by Slem · Nov 18, 2010 at 09:29 AM
public Transform[] ObjectsToSpawn; public float SpawnInterval= 15; public float MaxObjectsSpawned = 8; public float MinObjectsSpawned = 3;
private float _NextSpawn
void Start() { _NextSpawn = Time.time + SpawnInterval;} void Update() { if(Time.time >= _NextSpawn) { int objectsToSpawn = Random.Range(MinObjectsSpawned, MaxObjectsSpawned); for(int i < 0; i < objectsToSpawn; ++i) { Instantiate(ObjectsToSpawn(Random.Range(0, ObjectsToSpawn.Length), transform.position. Quaternion.identity); } _NextSpawn = Time.time + SpawnInterval; } }
You can also you the Invoke method which call a another method after x seconds.
Your answer
Follow this Question
Related Questions
How to make something happen over time? 1 Answer
how do i make my spawn system spawn every 2 seconds then wait 10minutes? 3 Answers
Instantiate 1 at a time at each transform in array 1 Answer
Spawn Prefab or FBX model after import? 1 Answer
How to fix my enemy Instantiate script? its clone too many 3 Answers