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
MediumMake the tank fire pooled bullets.
- In
Tank'sAwake, make anObjectPool<Bullet>: on when taken, off when released, destroyed when let go,maxSize: 4. - Space gets a bullet 1 unit ahead of the tank, handing it the pool.
Bulletflies +z atspeed; it releases itselflifeseconds after each shot, or on hitting aTarget(callHit()).
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
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular TurretRecap: Twin Cannon4Serialized Data
Serializable ClassesLists in the InspectorEnums in the InspectorRecap: Loot TableRecap: Roll the Loot2Interfaces in Unity
IDamageableInterfaces and TryGetComponentGetComponentsRecap: Interact KeyRecap: Nearest in ReachPractice on your own: Unity playground