# How to pool bullets for a double barreled gun with alternating fire?

I have created the gun with two barrels, which fire in alternation.

I have implemented pooling for one barrel, But I need to pool bullets in both barrels when firing in alternation.

This is my Bullet.cs script:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour {
private
Fire_Bullet _gun;

public
void SetGun(Fire_Bullet gun) = > _gun = gun;

private
void OnCollisionEnter(Collision collision) {
gameObject.SetActive(false);
_gun.AddToPool(this);
}
}


This is Fire_bullet.cs script attached to the gun:

using System.Collections.Generic;
using UnityEngine;

public class Fire_Bullet : MonoBehaviour
{

public Transform barrel_L_SpawnPoint;
public Transform barrel_R_SpawnPoint;

[SerializeField] private Bullet bullet_prefab;
[SerializeField] private float _delay =0.2f;
[SerializeField]  private float _bulletSpeed = 5f;

private float nextFire = 0f;
private bool shootSequance = false;
private Queue<Bullet> _pool = new Queue<Bullet>();
private Queue<Bullet> _poolTwo = new Queue<Bullet>();

// Start is called before the first frame update
void Start()
{
if (barrel_L_SpawnPoint == null || barrel_R_SpawnPoint == null)
{
Debug.LogError(" Barrel Spwan point is Mossing");
}
if(bullet_prefab == null)
{
Debug.LogError("Bullet Prefabs is Missing");
}

}

// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
nextFire = Time.time +_delay;
var bullet = GetBullet();
bullet.transform.position = barrel_R_SpawnPoint.position;
bullet.transform.rotation = barrel_R_SpawnPoint.rotation;
bullet.transform.position = barrel_L_SpawnPoint.position;
bullet.transform.rotation = barrel_L_SpawnPoint.rotation;
bullet.GetComponent<Rigidbody>().AddForce(barrel_R_SpawnPoint.up * _bulletSpeed, ForceMode.Impulse);
bullet.GetComponent<Rigidbody>().AddForce(barrel_L_SpawnPoint.up * _bulletSpeed, ForceMode.Impulse);
}
}

private Bullet GetBullet()
{
if (_pool.Count > 0 && shootSequance == false)
{
var bullet = _pool.Dequeue();
bullet.gameObject.SetActive(true);
return bullet;
}
else
{
var bullet = Instantiate(bullet_prefab, barrel_R_SpawnPoint.position, barrel_R_SpawnPoint.rotation);
bullet.SetGun(this);
shootSequance = true;
return bullet;
}

}

private bool PlayerCanShoot()
{
return Time.time >=  nextFire;
}

internal void AddToPool(Bullet bullet)
{
_pool.Enqueue(bullet);
}
}


## 1 Answer

There is no good reason why you would use separate pools for both barrels. You can have both of them drain from the same pool.

Your current code takes one bullet from the pool. Then it sets it up with all the properties of the left barrel. But then it overwrites all those settings it just set and overwrites them with those of the right barrel. This makes no sense.

I think what you actually want to do is to use either the properties of the left spawn point or the properties of the right spawn point , depending on which barrel's turn it is to fire. How do you keep track of which spawn point is the next one to fire? By adding another variable. Let's call it

private Transform active_SpawnPoint;


Now in your Update method, you need to set which spawn point is the active one:

    if (Input.GetMouseButtonDown(0))
{
if (active_SpawnPoint == barrel_L_SpawnPoint) {
active_SpawnPoint = barrel_R_SpawnPoint;
} else {
active_SpawnPoint = barrel_L_SpawnPoint;
}


This will toggle which barrel is the active one each time the player presses the left mouse button. Then, in the following code which does the bullet spawning, you use the properties of whichever spawn point is the active_SpawnPoint:

        var bullet = GetBullet();
bullet.transform.position = active_SpawnPoint.position;
bullet.transform.rotation = active_SpawnPoint.rotation;
bullet.GetComponent<Rigidbody>().AddForce(active_SpawnPoint.up * _bulletSpeed, ForceMode.Impulse);


• It worked well. Thank You Philipp Jul 29 at 6:19