- Home /
Help with Enemy Spawner!
Hi, so I am trying to make code for a Spawner that spawns my enemy prefab after 300 seconds and spawns 15 enemies. Then after 900 seconds it spawns 15 more enemies.Javascript would be preferred. Any help would be great thanks!
public var enemy : GameObject;
public var WaitTime : int;
function Start () {
yield WaitForSeconds(WaitTime);
InvokeRepeating("SpawnEnemy", 3, 3);
}
function SpawnEnemy()
{
//Instantiate the enemy prefab
var enemyClone : GameObject = Instantiate(enemy, Vector3.zero, Quaternion.identity);
}
This is the code I am using for the Spawner, it spawns the enemy at 0,0,0 how Do I make the enemy spawn on the object that I place this code on.
Answer by Piflik · Jan 06, 2013 at 12:14 AM
Instead of Vector3.zero use transform.position...
Hey thanks that worked but now it spawns 8 enemies every 3 seconds how do I get it to spawn 1 enemy every 3 seconds
I can not see anything in the code you provided that would cause 8 enemies to spawn simultaneously. Are you sure you don't have 8 of these spawners sitting on top of each other or one spawner with 8 instances of this script?
Answer by bzerk122 · Jan 06, 2013 at 03:47 AM
I personally think you should do something like this:
public var enemy : GameObject;
public var WaitTime : float;
//new timer
private var timer : float;
function Update(){
if(timer < WaitTime){
timer+=Time.DeltaTime;
}
if(timer>=WaitTime)
{
timer = 0;
//Instantiate the enemy prefab
var enemyClone : GameObject = Instantiate(enemy, transform.position, Quaternion.identity);
}
}
I don't know if this work i usually write in c# but that should work(Its count to wait time then instantiates then resets)
Answer by jeffbori · Oct 13, 2016 at 04:35 PM
i think to use yield WaitForSeconds(WaitTime); you need to start a coroutine first?
Your answer
Follow this Question
Related Questions
Access a instantiated gameobject from a enemy spawner 4 Answers
Spawn game objects on a timer 2 Answers
Adding a timer to spawner 2 Answers
Spanwer Script only spawns when Player is close and more. 1 Answer
Adding a counter? 1 Answer