- Home /
Question by
DAlturG · Apr 22, 2016 at 07:25 PM ·
scripting problemrandomobject pool
random Object pooling problem
I'm trying to pool random objects such that there would be an array of objects and that the script would pick any random ones and put it into the pool. I made a start on the object pool script and the destroy script for the objects but these don't seem to be working. When the Invoke method "Destroy" is called, nothing happens and none of the objects are set inactive. Here's my destroy and object pooling scripts.
ObjectPooling:
public float fireTime = 0.5f; public GameObject bullet;
public int pooledAmount = 20;
public List<GameObject> cars;
public Transform where;
int index;
bool active;
void Start()
{
cars = new List<GameObject> ();
for (int i = 0; i < pooledAmount; i++) {
GameObject obj = (GameObject)Instantiate (bullet);
obj.SetActive (true);
cars.Add (obj);
}
InvokeRepeating ("Fired", fireTime, fireTime);
}
void Fired()
{
for(int i = 0; i < cars.Count; i++)
{
if(!cars[i].activeInHierarchy)
{
cars[i].transform.position =new Vector3(0,0,0);
cars[i].transform.rotation = where.rotation;
cars[i].SetActive(true);
break;
}
}
}
destroy:
void OnEnable()
{
Invoke("Destroy",2f);
}
void Destroy(){
this.gameObject.SetActive (false);
}
void OnDisable()
{
CancelInvoke ();
}
Comment
Your answer