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 jadde_dahlberg · Sep 19, 2018 at 10:09 AM · gameobjectprefabcodepagepoolingpool

Pooling Issue

Hey there!

I have recently used a simple forloop to instantiate a world on runtime. It was working perfectly without lag or anything on my phone and computer.

However I wish to optimize it some more and I instantly got brought in to the world of object pooling. So I created a simple script that Instantiates in front of the player as he moves and removes the ones that are far behind.

This is where I started pooling them instead of using Destroy. Now everything seemed to work fine as I tested pooling.

The objects get deactivated instead of destroyed behind me, they get put into a GameObject List. And reactivated in front where the new one is supposed to be.

However after a couple of seconds in to the game. It just stops. But thats not the whole deal here because they do get activated in front of the player still, but only for a split second. Then it deactivates itself again immediately.

They remain pooled for some reason. I tried writing it myself, i tried other peoples scripts and I came to the conclusion that this one worked the best. But I still get the deactivation problem. Here it is:

     public GameObject[] platformPrefabs;
     public bool willGrow = true;
 
     WorldGenerator generator;
 
     public List<GameObject>[] pooledObjects;
 
     void Start()
     {
         generator = GameObject.Find("World GENERATOR").GetComponent<WorldGenerator>();
 
         platformPrefabs = generator.WorldObjects;
 
         /*for (int i = 0; i < generator.WorldObjects.Length; i++)
         {
             platformPrefabs[i] = generator.WorldObjects[i];
         }*/
 
         // instantiate our pool (empty)
         pooledObjects = new List<GameObject>[platformPrefabs.Length];
         for (int i = 0; i < platformPrefabs.Length; i++)
         {
             pooledObjects[i] = new List<GameObject>();
         }
     }
 
     public GameObject GetPooledObject()
     {
         int randomIndex = Random.Range(0, pooledObjects.Length);
 
         for (int i = 0; i < pooledObjects[randomIndex].Count; i++)
         {
             GameObject go = pooledObjects[randomIndex][i];
 
             if (!go.activeInHierarchy)
             {
                 return go;
             }
         }
 
         if (willGrow)
         {
             GameObject obj = (GameObject)Instantiate(platformPrefabs[randomIndex]);
             pooledObjects[randomIndex].Add(obj);
             return obj;
         }
 
         return null;
     }

Thanks for the help!

EDIT

Here's what I use to get the object in another script:

                             GameObject newObject = theObjectPool.GetPooledObject();
 
                             newObject.transform.position = transform.position;
                             newObject.transform.rotation = transform.rotation;
                             newObject.SetActive(true);



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

148 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 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

pooled prefab missing child 1 Answer

Pooling GameObjects with TextMesh ? 0 Answers

Check if GameObjects are from the same prefab? 1 Answer

spawn from pool only if pool has inactive gameobjects 3 Answers

How should one give a gameobject to a prefab? 1 Answer


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