I basically want to Instantiate x total number of objects, with a y delay between each Instatiate. This script only spawns in one object, then stops. I would appreciate it if someone could give me an idea of what I could do to fix this.
var shipNumber = 5.0;
var spawnDelay = 3.0;
var ship : GameObject;
function Start () {
if (shipNumber <= 5.0);
Instantiate(ship, transform.position, transform.rotation);
yield new WaitForSeconds (spawnDelay);
}
The Start function only gets called once and you shouldn’t use coroutines/yield statements in there anyway. Additionally, you never update shipNumber nor does your if check look quite right.
Try this instead:
var shipNumber = 0.0;
var spawnDelay = 3.0;
var ship : GameObject;
function Start () {
SpawnShips();
}
function SpawnShips () {
while (shipNumber <= 5.0) {
Instantiate(ship, transform.position, transform.rotation);
shipNumber++;
yield new WaitForSeconds(spawnDelay);
}
}
The above is untested forum code but should prove the point:
- Call a custom function to spawn your ships
- That function should execute a while loop to “do your work”.
- That “work” involves spawning a new ship, incrementing the ship count, then yielding, when done you’ll exit the loop.
Done!
here’s how I did mine,
var numberOfShips = 0;
var shipPreFabWave2A : Transform;
var shipPreFabWave2B : Transform;
var maxShip = 10;
var counter = false;
function Update()
{
if(ScoreGUI.instance.class1Wave == 2)
{
if(numberOfShips < maxShip counter == false)
{
counter = true;
//shipDistance = false;
CreateCloneScout1();
}
}
if(numberOfShips == maxShip)
{
ScoreGUI.instance.class1Wave = 3;
this.enabled = false;
}
}
function CreateCloneScout1()
{
numberOfShips ++;
numberOfShips ++;
Instantiate(shipPreFabWave2A,transform.position,transform.rotation);
ScoreGUI.instance.shipCounter ++;
Instantiate(shipPreFabWave2B,transform.position,transform.rotation);
ScoreGUI.instance.shipCounter ++;
yield WaitForSeconds(1.0);
counter = false;
}
hope it helps.
Edit: Tom’s code is shorter, I think it’s better than what you are trying to do
Thank you for the rapid assist, gentlemen! That code works like a charm, HiggyB-I feel embarassed that I was so close and couldn’t see the answer, but at least I was on the right track!
If I had $1 for every time I’ve been in that same spot I’d be a wealthy man! No worries, I’m just glad we got you sorted and on the way so easily. 8)