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
1
Question by theunsigned · Jan 30, 2020 at 11:26 PM · instantiateprefabinstantiate prefabpoolingpool

spawn from pool only if pool has inactive gameobjects

Hi, I've been searching for about a week now but can't quite figure out an elegant solution to my problem.

I'd like to instantiate a gameobject from various pools only if there is a gameobject in the pools that is currently inactive.

For example, if there is a population number (300) and I have 250 active gameobjects from that population, how might I enable one but not spawn one if the population is 300. Again, I'm using different pools (young, middle-aged, old) and want to make sure that I don't disable active game objects. I'm working with hundreds of gameobjects and want to be as performant as possible but I'm running into a mental block on this and appreciate any general guidance available. I think I'm misunderstanding something very basic because it seems that making a population int and comparing it every time is not as straight-forward as it could be.

Thank you.

Comment
Add comment · Show 1
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
avatar image NVJOB · Jan 31, 2020 at 01:42 AM 3
Share

I have a Simple Pool asset, it is very simple and free, there is a setup video, you can download this asset in the unity asset store - https://assetstore.unity.com/packages/tools/integration/nvjob-simple-pool-156582

Instruction - https://nvjob.github.io/unity/nvjob-simple-pool

There are two examples in the asset.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by logicandchaos · Jan 31, 2020 at 03:16 AM

When I do pooling I have a list for all the inactive objects. Usually a list for active objects is not needed, but if it is than that is easy enough to add. Anyways I use OnEnable() and OnDisable() to add and remove objects from lists. So then when I need to add an object from the pool I just grab the one at [0] and if the pool is empty then I instantiate a new object, if that is what is required.

Comment
Add comment · Show 1 · Share
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
avatar image theunsigned · Jan 31, 2020 at 10:20 AM -1
Share

Ah, you have really helped me understand where I've been lost. I was following the Brackeys @AT-Brackeys (?) pooling tutorial, which is designed for different functionality. I like that it uses its own tagging system to easily reference the pools and I've tried adapting it to fit my needs. Does this code make sense or am I still not understanding how to properly use this Dictionary and List combination? It seems to work for spawning up to the limit of the pool but I can't get it to draw from the disabled pooled objects once they are disabled. I may just go to a traditional pooling system and try to incorporate the tags but I appreciate any suggestions or references. Thank you!

     public GameObject SpawnFromPool(string tag, Vector2 position, Quaternion rotation)
     {
         if (!poolDictionary.Contains$$anonymous$$ey(tag))
         {
             Debug.LogWarning("Pool with tag " + tag + " doesn't exist.");
             return null;
         }
         
         //Brackeys - get prefab to spawn from dictionary - pull first element in queue
         GameObject objectToSpawn = poolDictionary[tag].Dequeue();
         //testing
         if (!objectToSpawn.activeInHierarchy)
         {
             //Brackeys - configure spawned object
             objectToSpawn.SetActive(true);
             objectToSpawn.transform.position = position;
             objectToSpawn.transform.rotation = rotation;
 
             poolDictionary[tag].Enqueue(objectToSpawn);
 
             return objectToSpawn;
         }
         //testing
         return null;
     }
avatar image
-1

Answer by rinomoen92 · Feb 16, 2020 at 08:26 PM

Bump Bump Bump Bump Bump Bump

Comment
Add comment · Share
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
avatar image
-1

Answer by steven1022 · Feb 16, 2020 at 08:46 PM

I used this tutorial the first time I implemented and it worked great

https://www.raywenderlich.com/847-object-pooling-in-unity

Comment
Add comment · Share
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

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

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

Destroying assets is not permitted to avoid data loss. 0 Answers

How to use pool to spawn object instead of instantiate? 2 Answers

My objects instantiate at a strange z coordinate 0 Answers

How to instantiate particle effect after destroying it 2 Answers

Instantiate is spawning too many clones 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