Question by
abeLincoln41 · May 16, 2017 at 04:19 PM ·
spawning problemspoolingplatformsobject poolinfinite runner
How can I fix my object pool to continue after original pool is exhausted?
I have an object pooler that will create objects and successfully activate and deactivate objects and place them on the screen until it is time to start going through the list from the beginning again. I am using a dictionary and queue to pool my objects. I have also noticed that even though I am not able to successfully pull my objects out of the pool after going through them, there is, in fact, an object next in line in the queue. Please help me figure out my problem! I have attached the object pooler code below:
public class ObjectPooler : MonoBehaviour
{
public Dictionary<int, Queue<GameObject>> poolDictionary = new Dictionary<int, Queue<GameObject>> ();
static ObjectPooler _instance;
public GameObject platformBrown;
public GameObject platformPink;
int poolSize = 10;
public static ObjectPooler instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<ObjectPooler>();
}
return _instance;
}
}
public void Start()
{
createPool(platformBrown, poolSize);
createPool(platformPink, poolSize);
PlayerPrefs.SetString("Original", "Yes");
}
public void createPool(GameObject platform, int poolSize)
{
string tag = platform.tag;
int poolKey = -1;
poolKey = (tag == "BrownPlatform" ? 0 : 1);
poolDictionary.Add(poolKey, new Queue<GameObject>());
for (int i = 0; i < poolSize; i++)
{
GameObject newObject = Instantiate(platform) as GameObject;
newObject.SetActive(false);
poolDictionary[poolKey].Enqueue(newObject);
}
}
public void reuseObject(GameObject platform, Vector2 placement, Quaternion rotation)
{
string tag = platform.tag;
int poolKey = -1;
poolKey = (tag == "BrownPlatform" ? 0 : 1);
GameObject toReuse = poolDictionary [poolKey].Dequeue();
poolDictionary[poolKey].Enqueue(toReuse);
toReuse.SetActive(true);
toReuse.transform.position = placement;
toReuse.transform.rotation = rotation;
Debug.Log(poolDictionary[poolKey].Peek());
}
}
public class PlatformGenerator : MonoBehaviour {
float waitingTime; // Variable field for the random number that will determine platform spacing.
float timer;
float yPos; // Variables for Position of next platform.
float xPos;
public GameObject newPlatform; // Variable for the next platform spawned by current platform.
public ObjectPooler pool;
public float posX; // Variables for deleting the platform if it is offscreen.
public float endScreen = -300;
void Start () {
waitingTime = Random.Range(1f, 1.35f);
newPlatform = this.gameObject;
}
void Update () {
timer += Time.deltaTime;
if (timer > waitingTime)
{
createPlatform();
timer = -100;
}
ensurePosition();
deleteOff();
}
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);
Vector2 nextPlacement = new Vector2(xPos, yPos);
pool.reuseObject(newPlatform, nextPlacement, Quaternion.identity);
}
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);
}
}
private void deleteOff()
{
posX = transform.position.x;
if (posX < endScreen)
{
gameObject.SetActive(false);
}
}
}
Comment