This post has been wikified, any user with enough reputation can edit it.
Question by
Zack148 · Sep 17, 2016 at 03:33 PM ·
objectpoolingobject pool
Object Pooling Help
I have been following GamesPlusJames endless runner tutorial link textand I have ran into a problem After creating the script it creates all of my clone platforms but only creates one in game and it disappears and moves as soon as the player gets close any help would be greatly appreciated!
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class ObjectPooler : MonoBehaviour {
public GameObject pooledObject;
public int pooledAmount;
List<GameObject> pooledObjects;
// Use this for initialization
void Start () {
pooledObjects = new List<GameObject> ();
for (int i = 0; i < pooledAmount; i++)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive (false);
pooledObjects.Add (obj);
}
}
public GameObject GetPooledObject ()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
if(pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive (false);
pooledObjects.Add (obj);
return obj;
}
}
Comment
Your answer
Follow this Question
Related Questions
Can someone post a generic object pool script? 4 Answers
Help with Object Pool Spawning. 1 Answer
Could I get an explanation of object pooling and how to do it? 2 Answers
Object pooling for many different Prefabs 2 Answers
Is instantiating empty gameObjects heavy on the CPU? ( Object pooling) 1 Answer