Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Object pooling

Discussion in 'Scripting' started by NeilB133, Aug 30, 2022.

  1. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    207
    Is there much benefit to upgrading my Unity version from 2020 to 2021 to get the update with the inbuilt object pooling (assuming this works for 2D games as well), or is building your own pooling system just as effective?
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,452
    It just depends. If your project is massive and you are worried about breaking things, updating to a newer version can be risky. If your project is pretty small and is mainly just code, I would say just update, its unlikely anything will significantly break.
     
    NeilB133 and DevDunk like this.
  3. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    249
    as Cornysam wrote, it's probably not worth upgrading just for the pooling unless you need to add pooling functionality to handle memory issues.

    2D, 3D makes no difference (ie yes it works for 2D games as well).

    But it does work, although the built-in pooling is at a more primitive level than what's available for third parties.

    I use it myself and it works fine although I built a wrapper around it to make it more like a "spawner" and add preloading, auto prefab despawning and so on.
     
    NeilB133 likes this.
  4. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    207
    Thanks for the advice guys, I'll create a custom pooling system I think then :)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Are you doing this recreationally or to grow your resume?

    Or do you know you have a problem that pooling will solve? And if not, why do it?

    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
     
    CentipedeSully likes this.
  6. flasker

    flasker

    Joined:
    Aug 5, 2022
    Posts:
    193
    always the same copy pasta

    some things are obvious to optimize in advance

    i dont need to try it to know that spamming instantiate is gonna lag

    same reason im not using gameobject.find or getcomponent on update until the game starts to drop performance enough that i have to go back and write code properly on my 1000 lines of code
     
  7. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    207
    The game I'm making has a ton of enemies that get instantiated and then destroyed every second by the player, so I'm just forward planning a little as I know down the road when levels in the game get crazy, performance will take a hit when things are constantly being instantiated and then destroyed. May as well create a system where game objects are taken from the pool, so enabled and then added back to the pool, so disabled, just seems a lot more suitable to the game I'm making :)
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,912
    That's totally fine. Though what most people here try to say, pooling may or may not improve performance depending on the platform and the number of objects and their complexity.

    On PC nowadays for moderate amount of objects it's usually not necessary to pool. So you will see barely any difference.

    This also makes some assumption which can easily turn out false. For example on mobile it was much more important to pool objects as instantiate was really slow on mobile (at least around 8 - 10 years ago, may no longer be an issue, idk). However activating and deactivating objects also had (still has?) a quite hit on performance. I've read about people not disabling the pooled objects but just moving them out of sight and just disable some scripts. Activating / deactivating does add / remove objects from various internal systems inside Unity. Again, this is most likely not an issue on modern hardware, but certainly was on some mobile devices back then.

    Just take my advice to not assume this or that action will improve performance, no matter how good your reason, because there are always things at play you didn't account for or didn't know about. There's a lot of black box magic behind Unity as well as the operating system or the hardware you're running on. Unless you're an expert in all 3 fields, it's quite hard to make reliable predictions if and how much a certain technique would improve your performance.
     
  9. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    207
    Perhaps I'll take yours and Dekker's advice and just wait off a little then. I thought that this would become a necessity down the line, but if pooling isn't required as much these days, I'll just wait until I see an issue with it then implement a fix then. I suppose I was just worried that down the line I'd have to refactor everything in order to now shoehorn pooling into the mix. Thanks for the reply though :)
     
    Bunny83 likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    As soon as you implement a pooling system, all of a sudden YOU become responsible for writing specific code zeroing out everything on those recycled objects.

    Sounds easy if they start out as "just a bullet" or something simple.

    But then you add a Rigidbody... oh now you need to have code that will set
    .velocity
    and
    .angularVelocity
    to zero before recycling the object. Oh wait, did I change the
    .drag
    field and
    .inertialTensor
    fields to get my boomerang feeling right? Now I have to reset that.

    And then use the pooling for an enemy with AI and animation... now the animation system has to be reset (forced to instant-transition to the known initial state), the AI has to be reset, and you forgot to zero out some aggro counter and now you have this weird subtle bug where recycled enemies are a little more dangerous... or their first frame shows them in the last pose they were in before they got recycled.

    And on and on... NavigationMeshAgent? What if you change the arrival distance for certain enemy types in certain conditions... and you forgot to reset it... "Bug: some enemies cannot reach the door." etc. etc. etc.

    Seriously, trust me, pooling objects can get really really complicated unless you are very disciplined about what objects get recycled and what state they have.

    You will need to maintain and debug and grow that reset code for EVERY different flavor of thing in play now, plus EVERY NEW THING forever into the future if it is on a pooled object.

    And in the end... oh look, it isn't even any faster anyway because Unity3D is already a beast of an engine!

    https://forum.unity.com/threads/object-pooling-in-a-tower-defense-game.1076897/#post-6945089
     
    Last edited: Oct 30, 2022
    _geo__, Nad_B, CentipedeSully and 4 others like this.