- Home /
NaN exception when spawning soldiers
Im getting a strange error randomly when they spawn, what is "transform.position assign attempt for 'spawnSoldier(Clone)' is not valid. Input position is {NaN, 0.262370.NaN}" i have no idea how to fix this :S
Heres my code
#pragma strict
var prefab:GameObject;
var soldierMax:int;
var soldierCount:int;
private var respawnPoints:GameObject[];
var maxSpawnDelay:int;
var soldiersKilled:int;
function Start () {
respawnPoints = GameObject.FindGameObjectsWithTag("SoldierSpawn");
soldierCount = GameObject.FindGameObjectsWithTag("Soldier").length;
}
function Update () {
if(soldierCount < soldierMax) {
respawnSoldier();
}
}
function respawnSoldier() {
while (soldierCount < soldierMax) {
Instantiate(prefab,respawnPoints[Random.Range(0,respawnPoints.Length)].transform.position,Quaternion.identity);
soldierCount++;
yield WaitForSeconds(Random.Range(3,maxSpawnDelay));
}
}
Answer by xtplpune · Apr 18, 2012 at 07:42 AM
Try with this code.........
var prefab:GameObject; var soldierMax:int=5; var soldierCount:int; private var respawnPoints:GameObject[]; var maxSpawnDelay:int=3; var soldiersKilled:int;
function Start () { respawnPoints = GameObject.FindGameObjectsWithTag("SoldierSpawn");
soldierCount = GameObject.FindGameObjectsWithTag("Soldier").length;
}
function Update () { if(soldierCount < respawnPoints.Length){ respawnSoldier();
}
} function respawnSoldier(){ while (soldierCount < respawnPoints.Length) { Instantiate(prefab,respawnPoints[soldierCount].transform.position,Quaternion.identity); soldierCount++; yield WaitForSeconds(Random.Range(0,maxSpawnDelay)); } }