Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

quick questions about object pooling

Discussion in '2D' started by Zarox, Oct 5, 2016.

  1. Zarox

    Zarox

    Joined:
    Nov 10, 2013
    Posts:
    42
    Hello.
    If im doing shooting game , do I need to create Object Pooling for my character's bullet, enemy's bullets, any special skills etc?
    Or I can use 1 script of Object Pooling for all the bullets?


    Thanks.
     
  2. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    This will depend, use object pooling if you have a lot of gameobject on the screen at anyone time, and your creating/destroying a lot of objects.

    I originally went for this approach but found that I needed a series of objects e.g. objects of objects(collection) so basically I created lets say 120 objects but I was not guaranteed to use a certain type, this was then overkill as extra memory was used that was not needed, so I diched it, if you only need a singe collection then it will be find.

    I was going to upload a pooler for you but realised it was the objects of objects version, which is a little more complicated.

    I would first try it without, as it does not take to much to change if you do it correctly, if then you see a lot of garbage collections they for sure, but it's not always the case.
     
  3. Zarox

    Zarox

    Joined:
    Nov 10, 2013
    Posts:
    42
    Thanke you for reply.
    Im not sure I understand , but I think i didnt explaind the question well.
    My question is , do I have to create more than one Object Pooling totally?

    So it will look like this : object pooling for my character bullets, another object pooling for the enemy bullets, another object pooling for special bullet skill etc, so it will ends up with serveral object poolings at the scene and each has his own Object Polling's script .

    ^ This is how its suppose to be ? ^

    Thanks again.
     
  4. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Hi Zarox,

    yeah my code is what your asking, again it will depend on how many objects you think you might have, I would of though for the bullets might be an idea as you could have many of these created in a short period of time.

    What I would do is create it first without pooling and see how it goes from there.

    There is many ways to speed up your game, in other ways
     
  5. Zarox

    Zarox

    Joined:
    Nov 10, 2013
    Posts:
    42
    So in short, if there are many objects(bullets) that in the scene at the same time I should make Object Pooling.
    And if there are small amout of objects 1-2 at the scene I should Instanitate and Destroy without object pooling.
    But im not sure how much objects are counted as 'many objects' to create for them object pooling.

    For example if i have around 5 object at the scene, should I make object pooling or instantiate and destroy?
    Or to make object pooling only if there are 20-30 at the same time?
     
  6. DalerHakimov

    DalerHakimov

    Joined:
    Mar 14, 2014
    Posts:
    302
    Profile your game and see how many bullets is many for your particular scene. Object Pooling is just one way of optimizing the game, if the scene doesn't have any other heavy animations and you have stable fps, you might not even want to do it.

    Regarding your original question, you can have one super class - bullets - then you can turn on /off some particular effect and it can become enemy, special, and etc bullet.. so in this case you just have to create one object pooling system.