- Home /
Question by
darkcookie · Mar 01, 2014 at 10:21 PM ·
for-looparray of gameobjectscycleeach
Instantiate From array, but cycle trough each gameobject
How can I spawn one game object from an array, but cycle trough each object in the array. Once it finished the cycle it repeats. Example: I have an array with (1,2,3,4,5) gameobjects, and i want to spawn #1, then once i call the spawn function again #2 and so on.. once it reached #5 it goes back to #1.
I had an Idea to do a for loop but that cycles trough all and every time it starts at 1.
var thingstospawn : GameObject[];
function Start(){
SpawnThing();
}
function SpawnThing(){
//for(i=0;i<thingstospawn.Length;i++){spawning code goes here } ????<-- original idea
}
Comment
Best Answer
Answer by Lo0NuhtiK · Mar 01, 2014 at 10:45 PM
Use an int to keep track of which object you spawned last time, and set it back to zero when it reaches the end of your array length.
pseudo-code :
var index : int = 0 ;
var things : GameObject[] ;
function SpawnThing()
{
//instatiate the thing
Instantiate(things[index]...) ;
//increment index
index ++ ;
//set index back to zero if we just spawned our last thing in the array
if(index > things.length-1)
index = 0 ;
}
oh ok thank you ! i see so you spawn based on a stored int. brilliant !