- Home /
Question by
jherrera1850_unity · Mar 28, 2020 at 12:49 AM ·
gameobjectdestroyspawn
Spawn and destroy gameobjectsfrom an array.,Spawn in and then destroy an object from a gameobject array
So im trying to create a script that will be able to spawn in an object and destroy from the array and then call in the next object and do the same thing. And on the last object of the array keep it and no destroy it. The problem i am running into is that the all the items seem to spawn all at once.
void Start()
{
for (int i = 0; i < rbctimesteps.Length; i++)
{
if (i < rbctimesteps.Length)
{
// yield WaitForSeconds(Random.Range (minSpawnTime, maxSpawnTime));
Destroy(Instantiate(rbctimesteps[i], transform.position, Quaternion.identity),
lifetime);
}
else
{
Instantiate(rbctimesteps[rbctimesteps.Length], transform.position, transform.rotation);
}
}
}
,So I'm trying to get an object from an array, spawn it, then destroy it after a set time and then spawn in the next object in the array. And once the script reaches the end of the array to not destroy the last object in that array. When i try to run it just spawns in the first item in the array and destroys it after the lifetime.
public GameObject[] rbctimesteps;
public float minSpawnTime = 1.0f;
public float maxSpawnTime = 10.0f;
public float lifetime = 1.0f;
// Start is called before the first frame update
void Update()
{
for (int i = 0; i < rbctimesteps.Length; i++)
{
if (i < rbctimesteps.Length)
{
Destroy(Instantiate(rbctimesteps[i], transform.position, Quaternion.identity),
lifetime);
}
else
{
Instantiate(rbctimesteps[rbctimesteps.Length], transform.position, Quaternion.identity);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612225914im_/https://answers.unity.com/themes/thub/images/avi.jpg)