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 /
avatar image
0
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
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I control Spawn speed/rate from an Object spawner? I'm new to Unity and scripting, and would like to delay the spawn rate of objects coming out of an ObjectPooler. 1 Answer

Deleting button when item removed from list? 1 Answer

Only want to change one item in list and not all of it's duplicates? 1 Answer

Object no longer spawn through object pooler 1 Answer

Enemy Health Doesnt Increase or Reset 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