- Home /
Script causing lag
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;
}
}
Comment
Answer by tanoshimi · Apr 20, 2016 at 06:17 PM
Yes. Object pooling.
Thanks, this looks like it would solve my problem but I have trouble making it spawn random objects. The script just pools one single random object from array ins$$anonymous$$d of pooling all random objects. Heres my script :
using UnityEngine; using System.Collections; using System.Collections.Generic; public class Fire : $$anonymous$$onoBehaviour {
public float fireTime = 0.5f;
public GameObject[] bullet;
public static float far = 0;
public int pooledAmount = 20;
public List<GameObject> cars;
public Transform where;
int index;
void Start()
{
cars = new List<GameObject> ();
for (int i = 0; i < pooledAmount; i++) {
GameObject obj = (GameObject)Instantiate (bullet[index]);
obj.SetActive (true);
cars.Add (obj);
}
InvokeRepeating ("Fired", fireTime, fireTime);
}
void Update(){
index = Random.Range (0, bullet.Length);
}
void Fired()
{
for(int i = 0; i < cars.Count; i++)
{
if(!cars[i].activeInHierarchy)
{
far = 0;
cars[i].transform.position =new Vector3(0,0,0);
cars[i].transform.rotation = where.rotation;
cars[i].SetActive(true);
break;
}
}
}
}