Menu
CoddyTech

Recap: Bullet Pool

Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 49 of 64.

Pooling keeps switched-off objects ready instead of destroying them. Here the tank's bullets come from Unity's ObjectPool and go back after every shot.

challenge icon

Challenge

Medium

Make the tank fire pooled bullets.

  1. In Tank's Awake, make an ObjectPool<Bullet>: on when taken, off when released, destroyed when let go, maxSize: 4.
  2. Space gets a bullet 1 unit ahead of the tank, handing it the pool.
  3. Bullet flies +z at speed; it releases itself life seconds after each shot, or on hitting a Target (call Hit()).

Press Play and shoot both targets. Then Check.

Try it yourself

using UnityEngine;
using UnityEngine.Pool;

public class Bullet : MonoBehaviour
{
    public float speed = 15f;
    public float life = 1f;

    // Fly forward; go back to the pool when it hits a Target, or after life seconds
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Game Architecture in Unity

Practice on your own: Unity playground