I have a spawning script that spawns objects in three different locations every x seconds. The objects place themselves on the right path after they are spawned as they have a separate script , but that isn’t the problem. This script is. Any suggestions on how it could be better and not cause any lag? Thanks
Script:
var power : Rigidbody[];
var carsL : Rigidbody[];/////spawns on left side
var carsM : Rigidbody[];/////spawns in middle
var carsR : Rigidbody[];///////spawns on right side
var spawnWaitTime1 = 0.5;
var spawnWaitTime2 = 0.5;
var spawnWaitTime3 = 0.5;
var prefabs : int;
var indexL : int;
var indexM : int;
var indexR : int;
var index : int;
public var where : Transform;
function Start ()
{
Invoke("Spawn1", 1);
Invoke ("Spawn2", 1);
Invoke ("Spawn3", 1);
spawnWaitTime1 = Random.Range(0.3,1);
spawnWaitTime2 = Random.Range(0.3,0.8);
spawnWaitTime3 = Random.Range(0.3,0.8);
}
function Spawn1 ()
{
prefabs = 1;
Invoke("Spawn1", 2);
if(prefabs == 1 ){
indexL= Random.Range (0, carsL.Length);
var object1L : Rigidbody = carsL[indexL];
object1L = Instantiate(object1L, where.position, where.rotation);
object1L.AddForce(Vector3(100,0,0));
if(indexL == 1){
spawnWaitTime1 = Random.Range(0.8,0.9);
}
}
if(prefabs == 2 && Random.value > 0.6){///// This hasn't been set yet
index = Random.Range (0, power.Length);
var powerup1 : Rigidbody = power[index];
powerup1 = Instantiate(powerup1, where.position, where.rotation);
powerup1.AddForce(Vector3(100,0,0));
}
if(prefabs == 2 && Random.value < 0.6){
prefabs = 1;
}
function Spawn2 ()
{
Invoke("Spawn2", 2);
prefabs = 1;
if(prefabs == 1 ){
indexM = Random.Range (0, carsM.Length);
var object1M : Rigidbody = carsM[indexM];
object1M = Instantiate(object1M, where.position, where.rotation);
object1M.AddForce(Vector3(100,0,0));
if(indexM == 1){
spawnWaitTime2 = Random.Range(0.8,0.9);
}
}
if(prefabs == 2 && Random.value > 0.6){
index = Random.Range (0, power.Length);
var powerup1 : Rigidbody = power[index];
powerup1 = Instantiate(powerup1, where.position, where.rotation);////for powerups
powerup1.AddForce(Vector3(100,0,0));
}
if(prefabs == 2 && Random.value < 0.6){
prefabs = 1;
}
}
function Spawn3 ()
{
Invoke("Spawn3", 2);
prefabs = 1;
if(prefabs == 1 ){
indexR = Random.Range (0, carsR.Length);
var object1R : Rigidbody = carsR[indexR];
object1R = Instantiate(object1R, where.position, where.rotation);
object1R.AddForce(Vector3(100,0,0));
if(indexR == 1){
spawnWaitTime3 = Random.Range(0.8,0.9);
}
}
if(prefabs == 2 && Random.value > 0.6){/////powerups
index = Random.Range (0, power.Length);
var powerup1 : Rigidbody = power[index];
powerup1 = Instantiate(powerup1, where.position, where.rotation);////for powerups
powerup1.AddForce(Vector3(100,0,0));
}
if(prefabs == 2 && Random.value < 0.6){
prefabs = 1;
}
}