0

When bulletpool is being instantiated, I can shoot bullets. But when all the bullets are done instantiated, I can't shoot the bullet. IDK where is the porblem..

BulletPoolScript:

public class scriptBulletPool : MonoBehaviour
{
    private static scriptBulletPool myInstance;
    public static scriptBulletPool MyInstance
    public GameObject bulletPrefab;
    public List<GameObject> bulletPool;
    public int poolSize;

    {
        get
        {
            return myInstance;
        }
    }

    private void Awake()
    {
        if (myInstance == null)
        {
            myInstance = this;
        } else if(myInstance != this)
        {
            Debug.LogError("Obj 1:", gameObject);
            Debug.LogError("Obj 2:", myInstance.gameObject);
        }

        instantiatePool();
    }

    private void instantiatePool()
    {
        bulletPool = new List<GameObject>();
        for (int i=0; i<poolSize; i++)
        {
            GameObject newBullet = Instantiate(bulletPrefab);
            bulletPool.Add(newBullet);
            newBullet.SetActive(false);

        }
    }

    public GameObject getBullet(Vector3 targetPos, Quaternion targetRot)
    {
        GameObject newBullet = bulletPool[bulletPool.Count - 1];
        newBullet.transform.position = targetPos;
        newBullet.transform.rotation = targetRot;
        newBullet.SetActive(true);
        bulletPool.Remove(newBullet);
        return newBullet;
    }

    public void returnBullet(GameObject bullet)
    {
        bulletPool.Add(bullet);
        bullet.SetActive(false);
    }
}

ShootScript:

public class scriptShoot : MonoBehaviour
{
    public GameObject bulletSpawnPoint;

    void Update()
    {
        shoot();
    }

    private void shoot()
    {
        if (Input.GetMouseButtonDown(0))
        {
            scriptBulletPool.MyInstance.getBullet(bulletSpawnPoint.transform.position, bulletSpawnPoint.transform.rotation);
        }
    }
}

BulletScript:

public class scriptBullet : MonoBehaviour
{
    public GameObject bullet;
    public float maxDistance;

    void Update()
    {
        transform.Translate(Vector3.forward * 7 * Time.deltaTime);
        maxDistance += 1 * Time.deltaTime;
        if (maxDistance >= 5)
        {
            scriptBulletPool.MyInstance.returnBullet(bullet);
        }
    }
}
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 1
    Your classes should really start with an uppercase letter – Jichael Aug 06 '19 at 11:07
  • the line `public static scriptBulletPool MyInstance` should probably come right before the `{ get...}` block – Ruzihm Aug 06 '19 at 11:23
  • Where do you have the scriptBullet component? on the bullets you are firing? Also did you check that your bullets return to your list? – S.Fragkos Aug 06 '19 at 11:25
  • When you debug it, what symptom do you have? empty list? errors? – BugFinder Aug 06 '19 at 12:38
  • public static scriptBulletPool MyInstance is just before { get...}, but here it is copy-paste mistake. – MrJanjua Aug 06 '19 at 13:11
  • Check the `poolSize` Is it Greater than `0`? if yes check if your bullets get recycled or not using the editor. You can check the `bulletPool` list in the editor. – AminSojoudi Aug 06 '19 at 15:03

1 Answers1

1

The problem is probably the scriptBullet.maxDistance.
You increment it in every update, and eventually pool the bullet.
When you need a bullet, you unpool it, but the maxDistance is never reset, resulting in the bullet being pooled again immediately.

Chillersanim
  • 455
  • 3
  • 13