- Home /
Question by
aidangig56 · Jan 02, 2014 at 06:53 PM ·
instantiatecommandzombie
How to Repeating command in js
I am trying to instantiate a zombie object every 1 seconds. but it only does it once how can I make it repeat continuously?
CODE: #pragma strict
var z : GameObject;
yield WaitForSeconds (1); Instantiate (z);
function Start () {
}
function Update () { }
Comment
Best Answer
Answer by HappyMoo · Jan 02, 2014 at 07:07 PM
function SpawnZombies()
{
while(true)
{
yield WaitForSeconds(1);
Instantiate(z);
}
}
function Start()
{
StartCoroutine(SpawnZombies());
}
Answer by YoungDeveloper · Jan 02, 2014 at 06:56 PM
This will spawn 10 zombies, each after 1 second
function Start(){
SpawnZombies();
}
function SpawnZombies(){
for(var i : int = 0; i < 10; i++){
//instantiate here
yield WaitForSeconds(1);
}
}
Answer by BigRoy · Jan 02, 2014 at 06:59 PM
It seems like you're performing the Instantiate function completely outside the Update method. Therefore it only runs it once upon class initialization.
Here's something that should work with the Update method:
var z : GameObject;
var spawnTime: float = 1;
private var lastSpawn : float = 0;
function Update () {
lastSpawn += Time.deltaTime;
if (lastSpawn > spawnTime) {
lastSpawn = 0;
Instantiate (z);
}
}
Or (as YoungDeveloper beat me to it) with yield in a coroutine:
var z : GameObject;
var spawnTime: float = 1;
function Start(){
SpawnZombies();
}
function SpawnZombies(){
while (true) {
Instantiate (z);
yield WaitForSeconds(1);
}
}
Your answer
