How can I use Stack to Instantiate Game Objects?

I hope the title was clear, but anyways.

I’ve been following Unity’s Space Shooter Tutorial , and I was showing it to some people in a gamedev topic.

One of them suggested that instead of calling the bullets and having them disappear upon going off-screen (which is how it works in the tutorial), it would be more efficient to instantiate a stack of bullet game objects from the start and recycle them.

At my present level, I don’t fully understand how to do this, but I would like to learn, and the person who suggested this has not been available for a while so I came here.

Sometimes finding thins on UA is about knowing the right words. The search term is ‘Object Pool’ and you will find a number of posts on the subject. The idea is that instead of Instantiating and Destroying objects all the time, keep a pool of objects and reuse them. A generic pool could store a number of different object types, and when a request for a specific object type finds it is missing in the pool, the pool will create more of the item, so that (apart from something like a memory failure), a request from the pool would never fail. Objects can cycle between being used and the pool.

But for your situation and (assumed) level of expertise, let’s start with something simpler. A Stack in computer programming means last in first out like a stack of dishes. Instead of a stack, I’m going to implement a circular queue so that the oldest item is always the one we get from the pool. One implemenation of a circular queue is to walk an array. When the walk reaches the end, start at the beginning again.

#pragma strict

var prefab : GameObject;  // Projectile prefab
var maxProjectiles = 50;
var pool : Transform[];
var iNext = 0;

function Start() {
	pool = new Transform[maxProjectiles];
	for (var i = 0; i < pool.Length; i++) {
	    pool *= Instantiate(prefab).transform;*
  • }*
    }

function GetNextProjectile() : Transform {
var ret = pool[iNext];
iNext = (iNext + 1) % pool.Length;
ret.rigidbody.velocity = Vector3.zero;
ret.rigidbody.angularVelocity = Vector3.zero;
ret.gameObject.SetActive(true);
return ret;
}

function Update() {

  • if (Input.GetMouseButtonDown(0)) {*
  •  var obj = GetNextProjectile();*
    
  •  obj.position = transform.position;*
    
  •  obj.rotation = transform.rotation;*
    

_ obj.rigidbody.AddForce(transform.forward * 1000);_

  • }*
    }
    Attach this script to an empty game object (or a visible one with the collider turned off). Create a prefab for your projectile. It must have a Rigidbody and be disabled. Drag and drop your projectile prefab on the ‘prefab’ slot. You can now infinitely fire bullets on the left mouse click without any Instantiates() or Destorys(). Add a bit of code to rotate the game object this script is attached to, and you can spray the scene.
    The pool stores the Transform of the objects. It could just as easily be written to store the GameObject or the Rigidbody since you can always get from one to the other. There is no pulling anything from the pool or returning it to the pool. The pool just assumes that the oldest one is ready for resuse. This works well for projectiles that have a short lifespan.
    One note with pooling. Something in either your pool code or your use code must return objects back to some initial state. For example the GetNextProjectile() set the velocity and angularVelocity. If the code did not do this, then the AddForce() would be combined with an initial velocity from the projectiles last use.