Here’s my script so far:
var prefabToSpawn:Transform[ ];
var spawnTime:float;
var spawnTimeRandom:float;
private var spawnTimer:float;
function Awake()
{
ResetSpawnTimer();
}
function Update()
{
if(spawnTimer > 0)
{
spawnTimer -= Time.deltaTime;
if(spawnTimer <= 0.0)
{
spawnTimer = 0;
RandomBuilding ();
Instantiate(RandomBuilding.buildingToSpawn, transform.position, Quaternion.identity);
ResetSpawnTimer();
}
}
}
function ResetSpawnTimer()
{
spawnTimer = spawnTime + Random.Range(0, spawnTimeRandom*100)/100.0;
}
function RandomBuilding () {
var newBuilding = 0;
if (prefabToSpawn.length > 0){
newBuilding = Random.Range(0,prefabToSpawn.length);
var buildingToSpawn = prefabToSpawn[newBuilding];
}
}
easy fix… and please use code tags when posting code.
In your RandomBuilding funciton, you create a variable called buildingToSpawn. This variable is never passed from the function. The fix is easy as you just don’t have to run that function.
I also simplified the whole thing so you are not using extraneous functions.
var prefabToSpawn:Transform[];
var spawnTime:float;
var spawnTimeRandom:float;
private var spawnTimer:float;
function Awake(){
spawnTimer = spawnTime + Random.Range(0, spawnTimeRandom*100)/100.0;
}
function Update(){
if (prefabToSpawn.length == 0) return;
if(spawnTimer <= 0.0){
var buildingToSpawn = prefabToSpawn[Random.Range(0,prefabToSpawn.length)];
Instantiate(buildingToSpawn, transform.position, Quaternion.identity);
spawnTimer = spawnTime + Random.Range(0, spawnTimeRandom*100)/100.0;
}
spawnTimer -= Time.deltaTime;
}
Thanks!
How do you use code tags?