Alternative to Creating and Destroying objects

Hello there. I am programming a 2D shooter game where lots of zombies will try to kill the player. I manage this situation like follows (very important to note: this game is supposed to be released on Android, that is, limited memory and CPU resources are available).

1.) In the scene editor, I create one zombie GameObject. This will be the initial (mould) zombie.

2.) After the game starts, time starts to count down until a new zombie is created. This time, “thresholdTime”, will be the time between new initializations of zombies. If I set thresholdTime=5, that would mean that after 30 seconds I would have dynamically instantiated 6 new zombies GameObjects. I store them in a List.

3.) Whenever the player kills a zombie, that zombie GameObject is destroyed.

I know this creating-destroying cycle is not the best way to approach my problem.

I have heard about Object Pooling but I don’t know how to implement this concept.

Finally I would like to tell you one possible workaround I thought about: just initialize a fixed number of zombies (say, 30 of them) and locate them at arbitrary (random) distances from the player. Whenever the player kills a zombie, I would not destroy that instance but rather “transport” that GameObject to a new, random position (out from screen, giving the sensation that this zombie has been killed). I don’t know if this is a good way: what if I need to increase the difficulty and instantiate new zombies?

What do you think?

Thanks

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

1 Like

Thanks @Dantus , I will have a look at it (didn’t know about the existence of a beginner module for Object Pooling).

More ideas? What do you think about the last paragraph I wrote in my first post?

Cheers

What you describe is a basic form of object pooling. The extra step you need is a Reset() function, or a Restart(parameters) function at the same time you teleport them away from the player. In that function you can change the difficulty of the enemies if you’d like.

For a true object pool you are almost there. Generally objects that get pooled implement an IPoolable interface that has Activate(), Deactivate(), and Reset() functions. Instead of Instantiating objects, you would ask the PoolManager for a new object, and the PoolManager decides if it has a deactive one ready to go, or needs to instantiate a new one.

1 Like

I will take a time to digest what you answered me, @GarthSmith . I will learn as much as possible about PoolManager. Thanks :slight_smile:

New ideas are welcome