- Home /
Question by
jpc133 · Apr 16, 2013 at 08:28 PM ·
spawning problemscrashes
wave spawn problems
My unity crashes when I use this script are there any problems with it?
`#pragma strict
public var prefab : Transform;
var spawnnumber = 1;
static var spawnhealth = 50;
var spawngap = 20;
var numberspawned = 0;
//var robot = Transform;
function spawnnow () {
Debug.Log("spawnnow");
numberspawned = spawnnumber;
if (numberspawned > 0){
Instantiate(prefab, transform.position, transform.rotation);
Debug.Log("1 spawned");
WaitForSeconds(1);
numberspawned = numberspawned - 1;
spawnnow();
}
if(numberspawned == 0) {
spawn();
}
}
function spawn () {
Debug.Log("spawning");
spawnnow();
WaitForSeconds(spawngap);
//spawngap = spawngap - 1;
spawnnumber = spawnnumber + 1;
}
function Awake () {
Debug.Log("started");
WaitForSeconds(1);
spawn();
}
`
Comment
Best Answer
Answer by Glister · Apr 16, 2013 at 11:04 PM
The code has no errors at first glance. I copied and pasted it looked fine I hit play and your right it literaly crashed unity. I had to force it closed to get out. but on closer inspection you do not have yield in front of waitsecond so I added that and it shines a light on your recursive errors where you have not declared a return type.
#pragma strict
public var prefab : Transform;
var spawnnumber = 1;
static var spawnhealth = 50;
var spawngap = 20;
var numberspawned = 0;
//var robot = Transform;
function spawnnow ():IEnumerator {
Debug.Log("spawnnow");
numberspawned = spawnnumber;
if (numberspawned > 0){
Instantiate(prefab, transform.position, transform.rotation);
Debug.Log("1 spawned");
yield WaitForSeconds(1);
numberspawned = numberspawned - 1;
spawnnow();
}
if(numberspawned == 0) {
spawn();
}
}
function spawn ():IEnumerator {
Debug.Log("spawning");
spawnnow();
yield WaitForSeconds(spawngap);
//spawngap = spawngap - 1;
spawnnumber = spawnnumber + 1;
}
function Start () {
Debug.Log("started");
yield WaitForSeconds(1);
spawn();
}
Thanks for the Answer it will help my project thanks! I'm quite new to unity and this is the fastest response Ive ever got.