I want to spawn a 100 object whit 0.1 seconds in between. I have this but they still spawn all at once
`` private float timer = 0.0f; bool goSpawn = false;
private void Update()
{
if (goSpawn)
{
timer = Time.deltaTime;
}
}
public void spawnAnimals()
{
goSpawn = true;
for (int i = 0; i < 100;)
{
if (timer > 2f)
{
RndSpawnPos = new Vector3(Random.Range(3.30f, 5.70f), 0.78f, Random.Range(-3.00f, 3.01f));
Instantiate(animal, RndSpawnPos, Quaternion.identity);
i ;
timer = 0.0f;
}
}``
CodePudding user response:
Use an IEnumerator
. I put an example to solve the problem:
public void Start() => StartCoroutine(SpawnObject(100, .1f));
public IEnumerator SpawnObject(int count, float intervalTime)
{
for (var i = 0; i < count; i )
{
Instantiate(" something in here...");
yield return new WaitForSeconds(intervalTime);
}
}