- Home /
Find inactive game objects in object-pool by tag?
I've created a pool of inactive objects and in certain points of the gameplay, I want some of these inactive objects to become active only if they have a certain tag. Unfortunately, you can't access a game object by tag if it's inactive. So it seems that I won't be able to make them active when I need them.
I tried this solution:
Instead of keeping the game objects inactive in the pool, I keep their mesh-renderer turned off. And when I need them, I can just enable their mesh-renderers. However, for this I have to use the GetComponent<> function repeatedly. This works fine but from what I read in other threads, this is very inefficient.
Are there any alternatives?
Answer by TheSaviour · Mar 31, 2018 at 04:39 PM
I've managed to find a way out of this problem. It turns out that even when an object is inactive, you can still access its transform values. Hence, you can run the Transform.childCount() method on it. So I decided to use the childCount() values as identifiers for objects in my object-pool. In other words, instead of only activating the game objects with a particular tag, I'm activating game objects with a certain childCount() value. Because in my pool, all the objects with the same tag have the same number of children. By following this strategy, I got just the results I wanted.
To be more specific, in my pool, I have 3 types of platforms: Main platform, Death platform and Unstable platform. Here's the function I created for retrieving an object from my pool:
public GameObject RetrivePooledObject (int children)
{
for(int i = 0; i < platformPool.Count; i++)
{
if(!platformPool[i].activeInHierarchy && platformPool[i].transform.childCount == children)
{
return platformPool[i];
}
}
if (children == deathPlatformPrefab.transform.childCount)
{
GameObject death = (GameObject)Instantiate(deathPlatformPrefab);
death.SetActive(false);
platformPool.Add(death);
return death;
}
if (children == unstablePlatformPrefab.transform.childCount)
{
GameObject unstable = (GameObject)Instantiate(unstablePlatformPrefab);
unstable.SetActive(false);
platformPool.Add(unstable);
return unstable;
}
GameObject main = (GameObject)Instantiate(mainPlatformPrefab);
main.SetActive(false);
platformPool.Add(main);
return main;
}
When I eventually need a platform, I call this function from the platform spawning script by throwing the desired childCount() value as an argument.
That's not clean. In code you should not depend on objects hierarchies. Imagine you change the child count of your objects, your code will be broken.
What i answered you was the cleaner way to access to them. No matter what change on your object, the pool will be working.
But if this work for you then continue. It's my advice (more than 5 years in Unity and coding)
That's why I created an array of platforms in the platform spawning script. The script randomly chooses an index from the array and finds the number of children the platform in that index has. It saves that number in a variable (`int nOfChildren = platformArray[randomlyChosenIndex].transform.childCount`) and calls this function with the said number as an argument like this: GameObject spawnedPlatform = objectPool.RetrivePooledObject(nOfChildren);
I probably should have mentioned beforehand that I'm actually trying to use my object pool to spawn platforms randomly. So this strategy will work even if I change the number of children for each platform (as long as I make sure that each type of platform have different number of children).
Answer by Martin_Gonzalez · Mar 30, 2018 at 10:04 PM
Why don't you set a tag in the gameObject script.
Image your gameObject pool is a bullets pool and you have gameObjects with the Bullet.cs script on it.
You can set an id to that script like bulletId or bulletTag or bulletCategory. So then you can do a Where operator filtering only who has x tag.
var myBigBullets = myInactiveBullets.Where(bullet => bullet.tag == "bigBullet");
But in this example, your lists must contains Bullet List<Bullet> myInactiveBullets
instead of gameObjects.
Alright I see where you're getting at but I feel like you need to elaborate a bit. For instance, what's the meaning of "bullet" in your code? Is it referring to the Bullet.cs script? If so, shouldn't it be capitalized?
I said it: "Image your gameObject pool is a bullets pool and you have gameObjects with the Bullet.cs script on it."
Bullet is just an example
I leave you a simple example.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class PoolExample : $$anonymous$$onoBehaviour {
public int poolSize = 10;
public GameObject myObjectPrefab;
public List<$$anonymous$$yObject> _myInactiveObjects = new List<$$anonymous$$yObject>();
public List<$$anonymous$$yObject> _myActiveObjects = new List<$$anonymous$$yObject>();
// Use this for initialization
void Start () {
for (int i = 0; i < poolSize; i++) {
var go = Instantiate(myObjectPrefab, Vector3.one * 1000, Quaternion.identity);
var goScript = go.GetComponent<$$anonymous$$yObject>(); // be sure the prefab has the script
_myInactiveObjects.Add(goScript);
}
}
$$anonymous$$yObject Get() {
if(_myInactiveObjects.Count<=0) Debug.Log("Inactive list is empty. Add more objects to the pool");
var activeObject = _myInactiveObjects[0];
_myActiveObjects.Add(activeObject);
_myInactiveObjects.Remove(activeObject);
return activeObject;
}
$$anonymous$$yObject GetWith(string tag) {
if(_myInactiveObjects.Count<=0) Debug.Log("Inactive list is empty. Add more objects to the pool");
var customInactiveObjects = _myInactiveObjects.Where(obj => obj.myTag == tag);
if(!customInactiveObjects.Any()) Debug.Log("Inactive list is empty. Add more objects to the pool");
//we take the object from this list
var activeObject = customInactiveObjects.First();
_myActiveObjects.Add(activeObject);
//but we remove it from the real inactive objects
_myInactiveObjects.Remove(activeObject);
return activeObject;
}
}
//This script must be in the object prefab you want to pool
public class $$anonymous$$yObject : $$anonymous$$onoBehaviour {
public string myTag;
}
Thanks. I've actually managed to find a simpler solution to my problem. I have posted it as an answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612155712im_/https://answers.unity.com/themes/thub/images/avi.jpg)