Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

101 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Compiler Error not all paths return a value - Object Pooling 1 Answer

Can someone post a generic object pool script? 4 Answers

question about object pooling an array and spawning 0 Answers

Could I get an explanation of object pooling and how to do it? 2 Answers

Object pooling for many different Prefabs 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges