- Home /
Object no longer spawn through object pooler
Right now, I am attempting to get a turret to continuously fire bullets and re-use any that are inactive. However, when I run the game in editor mode, the turret does shoot a few bullets, but then it stops spawning bullets that are inactive.
My object pooler is based on the following tutorial right at the bottom comment with all the code: https://www.raywenderlich.com/136091/object-pooling-unity
Code for turret firing bullet:
Notice the debug passes through each time when a new bullet gets created / active, but after a few successful creations, it stops working altogether.
Here is how the bullets get made:
Answer by Bunny83 · Mar 31, 2017 at 06:20 PM
That object pool implementation is inherently bad. A couple of days ago there was another question about pretty much the same pooling script. I posted a simpler and way better implementation over here.
However your main problem is most likely that you do not re-initialize your bullet. You call initializeBullet from "Awake". However Awake is only called once in the lifetime of an object. So deactivating / activating won't call it again. So either make initializeBullet public and call it after you aquired the bullet from the pool, or perform the initialization in OnEnable instead of Awake. OnEnable will be called each time the object is enabled / activated.
Another thing that doesn't make any sense in your code is that you wrap only a part of your code in that null-checking if statement. Either put everything that accesses the "bullet_clone" in the if or remove it completely. Removing it of course assumes that GetPooledObject never returns null.
Finally, don't post code as images / pictures. If you have trouble to get the syntax highlighting working here on UA, have a look at the user guide
Thanks, I figured that re-activated objects always go through OnEnable shortly after I observed the error. I am looking forward to learning more about Object Pooling anyways.
Your answer
Follow this Question
Related Questions
Object Pooling with setActive function help 1 Answer
How do you call child in group of elements? 2 Answers
physics.OverlapSphere colliders 1 Answer