- Home /
Question by
dirtbikeguy55 · Nov 08, 2014 at 12:58 PM ·
javascriptenemytimer
Spawn game objects on a timer
I have this script that spawns enemies, but they only spawn once. So what I was wondering is if there is a way to add a timer or something into this script to make it so they will keep spawing. Here is the script.
#pragma strict
// Instantiates a prefab in a grid
var prefab : GameObject;
var gridX = 5;
var gridY = 5;
var spacing = 2.0;
function Start () {
for (var y = 0; y < gridY; y++) {
for (var x=0;x<gridX;x++) {
var pos = Vector3 (x, 0, y) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
}
}
}
Comment
Answer by 767_2 · Nov 08, 2014 at 01:15 PM
use InvokeRepeating , this will create every 2 seconds
function Start () {
float startIn=1;
float every=2;
InvokeRepeating("spawn", startIn, every);
}
spawn(){
for (var y = 0; y < gridY; y++) {
for (var x=0;x<gridX;x++) {
var pos = Vector3 (x, 0, y) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
}
}
}
Answer by toddisarockstar · Nov 16, 2014 at 05:27 AM
var timer:float;
function Update(){
if (timer<0){Instantiate bad guys here;
timer=10;}
timer=timer-Time.deltaTime;
}
that should do it!