I’m following this Brakeys tutorial on object pooling:
And here is what my method that initially instantiates the objects looks like:
private void Start()
{
poolDictionary = new Dictionary<string, Queue<PooledUIObject>>();
foreach (UIPool pool in pools)
{
Queue<PooledUIObject> objectPool = new Queue<PooledUIObject>();
for (int i = 0; i < pool.size; i++)
{
GameObject obj = Instantiate(pool.pooledObject.gameObject, this.transform);
PooledUIObject pooledObject = obj.GetComponent<PooledUIObject>();
pooledObject.gameObject.SetActive(false);
objectPool.Enqueue(pooledObject);
}
poolDictionary.Add(pool.tag, objectPool);
}
}
It’s all working well, but I’m wondering, is there any way to avoid the GetComponent
call when doing something like this?
I want the PooledUIObject
component because that holds the text and image that I want to have access to but when I instantiate something it always just returns a GameObject
, so is there any way, using this object pooling technique, to avoid the (potentially hundreds) GetComponent
calls?