Can't fire a projectile from an object pool

I originally wrote my projectile fire script using instantiate but I’ve learned more on object pooling and recycling and how it’s much more efficient. Now I have a basic object pool that can contain multiple objects to pool and I’m having a hard time replacing instantiate with activating a free object in the pool. Here is the pool script:

    public GameObject[] objects;
	public int[] number;

	public List<GameObject>[] pool;


	void Start () {

		instantiate();
	
	}
	

	void instantiate(){

		GameObject temp;
		pool = new List<GameObject>[objects.Length];

		for(int count = 0; count < objects.Length; count++){

			pool[count] = new List<GameObject>();

			for(int num = 0; num < number[count]; num++){
				temp = (GameObject)Instantiate(objects[count]);
				temp.transform.parent = this.transform;
				pool[count].Add(temp);
			}

		}

	}

	public GameObject activate(int id){

		for(int count = 0; count < pool[id].Count; count++){

			if(!pool[id][count].activeSelf){

				pool[id][count].SetActive(true);
				return pool[id][count];

			}
		}

		return null;
	}

That’s attached to an empty game object where I also plug in the bullet prefab in the objects list and set the amount I want. Here is my shoot script which I tried to modify for the new object pool:

    public ObjectPooler pool;

	private Transform myTransform;

	private Transform cameraHeadTransform;

	private Vector3 launchPosition = new Vector3();

	public float fireRate = 0.2f;

	public float nextFire = 0;

	
	void Start () {
	
		myTransform = transform;

		cameraHeadTransform = myTransform.FindChild("BulletSpawn");

	}
	

	void Update () {

		if(Input.GetButton("Fire1") && Time.time > nextFire){

			nextFire = Time.time + fireRate;

			launchPosition = cameraHeadTransform.TransformPoint(0, 0, 0.2f);

			pool.activate(0, launchPosition, Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90,
			                                                  myTransform.eulerAngles.y, 0));


		}
	
	}

That gives me two errors:

  1. “No overload for method ‘activate’ takes ‘3’ arguments” for the line where I’m trying to activate a projectile and set its transform rotation
  2. If I comment that part out to get in game, clicking the fire button gives me
    “Object reference not set to an instance of an object”, referring to the “public GameObject activate(int id){” line from the ObjectPooler script

So my questions are, why isn’t my shoot script able to reference a bullet from the object pool and how do I set the bullet’s new transform after I activate it (where I used to just instantiate it)?

This is pretty basic programming 101. You should probably Google a few tutorials

  • On line 30 on the second set of code you call activate and give it 3 arguments.
  • On line 33 of the first set of code you define activate with 1 argument.
  • Computers are pretty amazing, but they won’t work with 3=1, hence your error.

Here is my suggested fix for the errors. Starting from line 30 of the second script:

// This gets you a reference to the activated GameObject
GameObject current = pool.activate(0);
// These two lines set its position and roatation
current.transform.position = launchPosition;
current.transform.rotation = Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90, myTransform.eulerAngles.y, 0);

I haven’t checked the rest of your script for errors or looked at how well this will perform. But it will fix the errors you are getting.

The other way to solve this problem would be to give the Activate() method 2 more arguments. The best way would be to overload it with both.