- Home /
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.
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.
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.
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;
}
Answer by steven1022 · Feb 16, 2020 at 08:46 PM
I used this tutorial the first time I implemented and it worked great
Your answer
Follow this Question
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