Object Pooling in Unity 2021 is Dope AF

Tarodev · 128K views · 2021 · 18:10

Open on YouTube

Game programming & enginesSoftware performance & optimizationtutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering Unity performance and spawning questions, as a walkthrough of the engine's own pooling classes instead of a hand-rolled pool.

Recommended by 3 people across 3 different months, 2022 to 2024.

What it is

A Unity tutorial by Tarodev on the built-in object pooling API added in Unity 2021. It walks through setting up an ObjectPool, shows the performance gain over instantiating and destroying objects, and briefly covers other pool classes such as GenericPool.

What people said

Recommends IndieGameDev · 2022-08 · Stack Overflow · on “Unity android game lagging while playing on phone”

You can find some premade libraries [LINK] this one was made by me a while ago or you can do it yourself or use the base library provided from Unity since version 2021 see this helpful YT video [this video] from TaroDev.

Recommends Obscure021 · 2023-01 · Stack Overflow · on “Creating an object relative to the position and rotation of the previous object”

@Hatemsla, you can use object pooling to spawn objects. It is well-optimized. If you don't know about Object Pooling in Unity, head over to Brackeys video [LINK] . You can also use the Unity 2021 built-in pooling system, instructions from Tarodev video [this video] .

Recommends ipodtouch0218 · 2024-07 · Stack Overflow · on “How can I add and subtract to a variable from multiple game objects without losing the accuracy?”

So I did manage to solve the issue but still don't know why the inconsistency was being caused. Previously I was using my own implementation of Object Pool by following a [LINK] in which a generic class was created to create pools which can then be accessed as needed. It works as expected but it is more of a C# solution than a Unity solution to object pooling. public static void EnqueueObject<T>(T item, string name) where T : Component { if (!item.gameObject.activeSelf) { return; } item.transform.position = Vector2.zero; poolDictionary[name].Enqueue(item); Debug.Log("item.GetType(): " + item.GetType()); item.gameObject.SetActive(false); } public static T DequeueObject<T>(string key) where T