- Home /
Question by
abeLincoln41 · May 14, 2017 at 07:12 PM ·
listsspawning problemsobject pool
Unity object pooling works for first ten seconds and then all objects go deactivated. How do I fix this?
I am using Unity and have created a simple infinite runner game. I have two types of platforms that spawn on the screen infinitely. When I was using instantiate and destroy the platforms worked fine. Now that I am pooling my objects I am having a problem. I have an object pooling script that creates the pool and has a method that grabs an object. When each platform has spawned that platform then spawns the next platform. The pool works at first, platforms spawn, deactivate, and then call the next platform; However, this does not last long before all platforms go deactivated. What could the problem be?
public class ObjectPooler : MonoBehaviour {
public GameObject pooledObject1;
public GameObject pooledObject2;
public int numPooled;
List<GameObject> pool1;
List<GameObject> pool2;
// Use this for initialization
void Start () {
pool1 = new List<GameObject>();
pool2 = new List<GameObject>();
// Creating pool for first type of object.
for (int i = 0; i < numPooled; i++)
{
GameObject obj1 = (GameObject)Instantiate(pooledObject1);
obj1.SetActive(false);
pool1.Add(obj1);
}
// Creating pool for second type of object.
for (int i = 0; i < numPooled; i++)
{
GameObject obj2 = (GameObject)Instantiate(pooledObject2);
obj2.SetActive(false);
pool2.Add(obj2);
}
}
// Grabs an object from the pool.
public GameObject getObject1()
{
// Retrieving the first type of object.
for (int i = 0; i <= pool1.Count; i++)
{
if (!pool1[i].activeInHierarchy)
{
return pool1[i];
}
}
GameObject obj1 = (GameObject)Instantiate(pooledObject1);
obj1.SetActive(false);
pool1.Add(obj1);
return null;
}
public GameObject getObject2()
{
// Retrieving the second type of object.
for (int i = 0; i <= pool2.Count; i++)
{
if (!pool2[i].activeInHierarchy)
{
return pool2[i];
}
}
GameObject obj2 = (GameObject)Instantiate(pooledObject2);
obj2.SetActive(false);
pool2.Add(obj2);
return null;
}
}
public class PlatformGenerator : MonoBehaviour {
// Variable field for the random number that will determine platform spacing.
float waitingTime;
float timer;
// Variables for Position of next platform.
float yPos;
float xPos;
// Variable for the next platform spawned by current platform.
public GameObject newPlatform;
public ObjectPooler pool;
// Variable for the collider.
public BoxCollider2D boxCollider;
/**
* Use this for initialization
*/
void Start () {
waitingTime = Random.Range(1f, 1.35f);
boxCollider = GetComponent<BoxCollider2D>();
}
/**
* Update is called once per frame
*/
void Update () {
timer += Time.deltaTime;
if (timer > waitingTime)
{
createPlatform();
timer = -20;
}
ensurePosition();
}
/**
* This method creates a new platform object when the counter is 0.
*/
public void createPlatform()
{
// Generate relatively random number for y position of new platform.
yPos = Random.Range(transform.position.y - 20, transform.position.y + 20);
xPos = 235;
// Create the new platform at given y coordinate.
//Instantiate(this, new Vector2(xPos, yPos), Quaternion.identity);
if (gameObject.tag == "BrownPlatform")
{
newPlatform = pool.getObject1();
newPlatform.SetActive(true);
newPlatform.transform.rotation = Quaternion.identity;
newPlatform.transform.position = new Vector2(xPos, yPos);
}
else
{
newPlatform = pool.getObject2();
newPlatform.SetActive(true);
newPlatform.transform.rotation = Quaternion.identity;
newPlatform.transform.position = new Vector2(xPos, yPos);
}
}
/**
* This method ensures the y position is appropriate.
*/
private void ensurePosition()
{
// Checks for yPos above the screen.
if (transform.position.y > 75f)
{
int offsetY = Random.Range(20, 100);
yPos = transform.position.y - offsetY;
transform.position = new Vector2(transform.position.x, yPos);
}
// Checks for yPos below screen.
if (transform.position.y < -75f)
{
yPos = transform.position.y + 5;
transform.position = new Vector2(transform.position.x, yPos);
}
}
}
Comment