Search Unity

Help with object pooling for 2D endless Runner

Discussion in '2D' started by Shaniel29, Jun 19, 2017.

  1. Shaniel29

    Shaniel29

    Joined:
    Apr 27, 2017
    Posts:
    1
    I can't seem to grasp Object pooling. I need to make a 2D endless runner Game for school. Now I'm stuck! Help?
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I'm a fan of this tutorial: http://catlikecoding.com/unity/tutorials/object-pools/

    Check it out and let us know if there are any specific parts that you need help with.

    Here's the basic idea-- when you Destroy objects (among other things), unused space in memory remains allocated. These unused spaces accumulate, and are periodically freed for use with garbage collections. This is good, because otherwise there would be no memory left to use for anything. However, the more space there is that needs to freed up by a garbage collection, the longer a garbage collection takes. If there is a lot of "garbage" every time a GC is called, the game will slow down significantly. One common thing that can cause this is Destroy-ing high amounts of objects-- think things like bullets, lots of objects being created and removed.

    Pooling means that instead of using Destroy to remove frequently used objects, these objects are deactivated and catalogued by a pooling controller, which will deliver references to available objects for object generators to re-use. This will reduce garbage and also save time associated with creating the objects.

    For example, a gun object when firing will first poll the pooling controller to ask if there are unused bullet objects. If so, the pooling controller will remove that object from its list of available, pooled objects, and the bullet will be reactivated and positioned at the "creation" point, then initialized.
     
    Last edited: Jun 20, 2017