- Home /
Instantiate an array of gameobjects with a time delay between each
I've found some solutions similar to what I am looking for but they don't quite answer my question. The following is the criteria I'm trying to meet:
I'm trying to spawn an array of game objects with a time delay between each.
The number of items in the array is given by a variable that updates as the number of items in the array changes
I'm calling the method in update because the items can't spawn at start, it is only after a given time delay they will start spawning
They must stop spawning once every element in the array has spawned
They must spawn in order element[0], element[1], etc.
I tried to do something like the following:
private float countdown;
private float timeBetweenSpawns;
private int counter = 0; // this updates depending on the number of items given to the array
void Start()
{
countdown = timeBetweenSpawns;
}
void Update()
{
for(int i = 0; i < counter; i++)
{
if(countdown <= 0)
{
Instantiate(array[i], transform.position, quaternion.identity);
countdown = timeBetweenSpawns;
else
{
countdown -= time.deltatime;
}
}
the main issue with this is that it keeps repeating, and they do seem to spawn out of order at times, I'm not sure why I can't really find a pattern to the mayhem.
I then tried to do something like this:
for (int i = 0; i < clueCounter; i++)
{
StartCoroutine(WaitABit());
Instantiate(_colors[i], transform.position, Quaternion.identity);
}
IEnumerator WaitABit()
{
yield return new WaitForSeconds(5);
}
this spawns them in the order they are supposed to be spawned but because I have to call it in update the game objects are constantly spawning. I tried adding in some conditions that will only allow it to spawn once but nothing I'm doing seems to work and it just ends up messing with the order they spawn in.
Anyone have some tips for me to try, this seems like a simple concept but it's giving me some grief.
You don't need to loop through, just do this.
private int spawnedCount = 0;
void Update()
{
if(countdown <= 0)
{
if (spawnedCount +1 < array.Length)
{
Instantiate(array[spawnedCount++], transform.position, quaternion.identity);
countdown = timeBetweenSpawns;
}
}
else
{
countdown -= time.deltatime;
}
}
Answer by carl010010 · Feb 21, 2019 at 04:16 AM
This is off the top of my head but something like this should work
void Start()
{
//Start Spawning right away
StartCoroutine(WaitABit());
}
IEnumerator WaitABit()
{
//Spawn as many (clues?) as you nee
for (int i = 0; i < clueCounter; i++)
{
Instantiate(_colors[i], transform.position, Quaternion.identity);
//Wait awhile before spawning new items
yield return new WaitForSeconds(timeBetweenSpawns);
}
}
For a better explanation then I can give I would take a look at the unity coroutines tutorial https://unity3d.com/learn/tutorials/topics/scripting/coroutines?playlist=17117
This would occur after a delay from start correct? However I need to be able this sequence to stop and then start up again mid-game. To my understanding, if it's called in start like this once it executes it's done and you won't be able to call it again after the game is running.
you can call StartCoroutine(WaitAbit()) whenever you want. and that will delay an amount set by timeBetweenSpawns is exactly what you asked.
Your answer
Follow this Question
Related Questions
How can I instantiate a runtime created object 1 Answer
Vector3.Lerp curving along three destinations 1 Answer
Checking for objects and adding to array 1 Answer
In a group of gameobjects, how to have only one object active at a time while disabling the others? 2 Answers
Coroutine starts but doesn't run? 1 Answer