- Home /
Too many enemies spawned
I am trying to make a basic fps that spawns an enemy every three seconds. Currently it simply spawns hundreds of enemies endlessly. Here is my code: #pragma strict var timer: float =0.0; var isSpawning: boolean = false; var enemyPrefab: Rigidbody;
function Update () {
if(!isSpawning){
timer += Time.deltaTime;//Run a timer
}
if(timer >= 3){
Spawn();//When the timer is greater than 2.7 run the spawn function
}
}
function Spawn(){
//isSpawning = true;
for (var i = 0; i < Random.Range(1, 4); i++){
var Pos = Vector3(-5, 0.5, Random.Range(-1.4, 1.4));//Where to spawn enemies
var newEnemy: Rigidbody = Instantiate(enemyPrefab, Pos, Quaternion.identity);//Spawn
newEnemy.tag = "enemy";//Tag the new enemy
timer = 0;//Reset the timer
isSpawning = false;
}
}
Does anyone see a problem or have a better script? thanks.
Answer by fafase · Jul 10, 2013 at 01:24 AM
I don't see the problem but I have a better script. It actually has been here many times, you should look a little more but well here it is:
function Start(){
InvokeRepeating("Spawn",0.01f,3.0f);
}
function Spawn(){
for (var i = 0; i < Random.Range(1, 4); i++){
var Pos = Vector3(-5, 0.5, Random.Range(-1.4, 1.4));//Where to spawn enemies
var newEnemy: Rigidbody = Instantiate(enemyPrefab, Pos, Quaternion.identity);//Spawn
newEnemy.tag = "enemy";//Tag the new enemy
}
and remove the whole thing in the Update
Your answer
Follow this Question
Related Questions
Choosing given numbers randomly 1 Answer
LAN playing game Controllers name + spawn point 1 Answer
How to spawn a muzzleflash 0 Answers
Random spawn points for my target prefabs? 2 Answers
Spawning objects at random Vector3 3 Answers