OBJECT POOLING in Unity

Brackeys · 448K views · 2018 · 17:23

Open on YouTube

Game programming & enginesSoftware performance & optimizationIndie game developmenttutorial

Why Game Development Stack Exchange recommends it

Game Development Stack Exchange users link it when answering or asking about spawning many objects, particles or bullets in Unity without hurting performance.

Recommended by 3 people across 3 different months, 2018 to 2019.

Why Stack Overflow recommends it

Stack Overflow users link it when answering Unity questions about spawning many similar objects (bullets, tiles, enemies) efficiently, and askers often say they built their pooler from it.

Recommended by 3 people across 3 different months, 2019 to 2023.

What it is

A Brackeys tutorial on setting up object pooling in Unity to avoid the cost of repeatedly instantiating and destroying objects. It walks through building an ObjectPooler script using a dictionary of queues and a singleton, and links to generic pooler libraries.

What people said

Recommends Daniel Lip · 2018-04 · Game Development Stack Exchange · on “Why do I keep getting an exception about "an element with the same key already exists" in this code?”

I'm trying to follow this tutorial about object pooling in Unity [this video] I got to minute 17:22. But when I run the game I get this exception: ArgumentException: An element with the same key already exists in the dictionary. System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.Queue`1[UnityEngine.GameObject]].Add (System.String key, System.Collections.Generic.Queue`1 value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:404) ObjectPooler.Start () (at Assets/Scripts/ObjectPooler.cs:31) This script is attached to a new empty GameObject: using System.Collections.Generic; using UnityEngine; public class ObjectPooler

Recommends Swati · 2019-08 · Game Development Stack Exchange · on “How to playlay multiple Particle Systems when colliding with multiple obstacles using object pooling?”

I don't know with whom this script is attached but I would say this job should be done by player itself and my way of doing this will be like this: public GameObject playerPartical; private void OnCollisionEnter(Collision collision) { GameObject particalObject = Instantiate(playerPartical, transform.position, transform.rotation); ParticleSystem partical = particalObject.GetComponent<ParticleSystem>(); partical.Play(); } As this will be attached to each player then "player 1" will get reference to Red particles and "player 2" will get reference to "Blue particles". Now by this way each player will be only responsible for themselves. But the best possible solution wold to Object pooling [VIDEO

Recommends virtouso · 2019-11 · Game Development Stack Exchange · on “Optimizing spawning 500 objects every 3 seconds”

in unity basically pooling means: instead of using Instatntiate() that makes new object in the scene that takes cpu performance to allocate, make all of needed object on the start of the scene and only enable and disable them and only make new object when all of you instantiated objects are active and are not ready to reuse. you can use a foreach or for loop to look for in-active object to use it on a list but its not much performant. using a static array is much more performant than a list. as i searched on unity its best to use queue to make pooling. just en-queue an object that is ready to use and de-queue when it gets active. just check this brackes link to check how its used: [this video]

Recommends nothing · 2020-05 · Stack Overflow · on “How to control the placement of GameObject in a scene programmatically in Unity”

Immediate fix: Use obj.SetActive(false) to temporarily disable the object and then use obj.SetActive(true) when you need the object to be active. Other solutions / best practices: Create the object you desire in the scene, save it as a prefab (prefabricated object) and then only instantiate it when you need it. Here's a link for further reading into the prefab system. [LINK] Object pooling is typically used when you will have a bunch of the same objects (like lasers, bullets, etc). Watching this video may be of help: [this video]

Recommends SeLeCtRa · 2021-01 · Stack Overflow · on “How to make a game object from several game objects in Unity?”

You gave us nothing about your game. It is something like piano tiles? If so, you should destroy the objects that instantiated when objects out of camera range. Also you do everything on update. Be careful about it! It works every frame not every second. For standard mobile that 30 update call per second. Also, if you wish to spawn a lot of object, you should use special algorithm like Object Pooling In Unity [this video] Also you set every variable on update. Is it really necessary? I don't think but you never give us a clue about your game. Good luck!

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 [this video] . You can also use the Unity 2021 built-in pooling system, instructions from Tarodev video [LINK] .

Cites Daniel Lip · 2019-02 · Stack Overflow · on “Why the cubes spawn effect is not the same in the video tutorial?”

The video tutorial: Object Pooling [this video] In the Hierarchy I have two empty GameObjects one named Object Pooler the seocnd named Cube Spawner. In the Object Pooler is attached a script name ObjectPooler: using System.Collections; using System.Collections.Generic; using UnityEngine; public class ObjectPooler : MonoBehaviour { [System.Serializable] public class Pool { public string tag; public GameObject prefab; public int size; } #region Singleton public static ObjectPooler Instance; private void Awake() { Instance = this; } #endregion public List<Pool> pools; public Dictionary<string, Queue<GameObject>> poolDictionary; // Start is called before the first frame update void Start() { poolDict

Cites Tim Hunter · 2019-07 · Stack Overflow · on “Why does this error keep popping up? Index out of range: Unity 2D/WebGL C#”

Sounds like you're having an issue with not having enough memory allocated to the game. May have to look at solutions to increase the allocated memory or decrease the amount of used memory. For the latter, may want to consider something like Object Pooling [this video] your bullets and enemies. As for the Index out of range, that may be coming from some script trying to reference an object after it was destroyed so maybe double check your logic that deals with objects which can be destroyed.

Cites Ghosty · 2020-07 · Stack Overflow · on “Instantiate predefined number of object along a raycast in Unity”

If you ask me I would say it is kinda easy if you know little bit of Math trickery. I'm not saying that I'm very good at Math, but once you get it it's kind of easy to pull off next time. Here if I try to explain everything, i won't be able to explain clearly. Take a look as the code below I've commented the whole code so that you can understand easily. Basically I used a Method called Vector2.Lerp() Liner Interpolation, which means that this method will return value between point1, and point2 based on the value of 3rd argument t which goes from 0 to 1 . public class TestScript : MonoBehaviour { public Transform StartPoint; public Transform EndPoint; public float spaceBetweenPoints; [Space]

Cites SushiWaUmai · 2021-01 · Stack Overflow · on “Unity3D: How to do object pooling without a spawner singleton”

Usually, if you use object pooling, you make a singleton like in this [this video] video. After seeing this [LINK] video, I discovered how messy singleton can be. Is there any other way to do object pooling without using singletons? I wanna instead use Events.