Implementing an object pool

If your GameController Interval is 0.5 seconds and the destroy timer is 15, then (do the math) there will be at most 30 balls in play at a time. Or less, if some have fallen over the edge. In that case, we do not need to make our app continuously allocate new memory for a new instance of BouncyBall, only to delete that object up to 15 seconds later. Too much instantiation and destroying of objects results in fragmented memory. Unity will periodically go through and clean this up, a computationally expensive process called garbage collection (GC) that is best to be avoided whenever possible.

Object pooling is when you create a list of reusable objects to be used in your game, rather than continuously instantiating ...

Get Unity Virtual Reality Projects - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.