- Home /
Whats wrong about what i am doing?
Hey guys, i am trying to get an enemy spawner to create an enemy every spawn time but for some reason it just creates one and then done. Any help will be helpful. Here is my code :
var spawntime = 2; var enemy : Transform;
function Start () { var i = 0;
for (i=0; i>-1; i++) { WaitForSeconds(spawntime); Instantiate(enemy,transform.position,transform.rotation); } }
function Update () { var randomx = Random.Range(-5,5); transform.position.x = randomx;
}
Just from the top of my head, WaitForSeconds() requires a yield statement, the variable i already has the value of zero so I don't see why "i = 0" in the for loop.About the for loop, your condition will always be true, you start with 0, you keep adding 1 to it , so it will always be greater than -1, it is an infinite loop.And in the Update() function...you know that is called many times per second, now think about what will happen .
Answer by zyzyx · Aug 11, 2012 at 11:27 AM
Try:
function Start(){
StartCoroutine("SpawnEnemy");
}
function SpawnEnemy()
{
while(true)
{
Instantiate(enemy,transform.position,transform.rotation);
yield WaitForSeconds(spawntime);
}
}
http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html
Your answer
Follow this Question
Related Questions
Having camera attack upon spawning player? 1 Answer
ai spawning please help 0 Answers
Changing spawning position with C# 2 Answers
Stop spawning on bool false and enable on true? 1 Answer
Gap Between Scrolling Background Objects 0 Answers