Search Unity

Object pooling is not working with bullets.

Discussion in 'Scripting' started by Mr_Janjua, Aug 6, 2019.

  1. Mr_Janjua

    Mr_Janjua

    Joined:
    Nov 10, 2018
    Posts:
    2
    Error: When bullets are being instantiated, I can shoot bullet, but when they all have instantiated, I can't shoot/ use these bullets again.

    bullet Script:


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

    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);
    }

    }
    }



    Shoot script


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

    public class scriptShoot : MonoBehaviour
    {
    //public scriptBulletPool bulletPoolObj;
    public GameObject bulletSpawnPoint;


    void Update()
    {
    shoot();
    }

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



    Bulletpool script


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

    public class scriptBulletPool : MonoBehaviour
    {
    public GameObject bulletPrefab;
    public List<GameObject> bulletPool;
    public int poolSize;

    private static scriptBulletPool myInstance;
    public static scriptBulletPool MyInstance
    {
    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);
    }

    }

     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Check in scriptBullet that you have correctly assigned bullet. If not, the bullet itself will not be returned, but null will be.

    You probably should have written (and I assume that the scriptBullet is attached to each bullet):

    scriptBulletPool.MyInstance.retur5nBullet(this.gameObject);

    Note also that an (on average) more performant pool manager should not remove and add bullets, simply inactivate and activate objects and leave them in the pool. Get would simply search for the first inactive object in the Array, and if there are none, it would add a couple more and inactivate them.

    Also, make sure you implement OnEnable and OnDisable for your bullets if you make them more complex.
     
  3. Mr_Janjua

    Mr_Janjua

    Joined:
    Nov 10, 2018
    Posts:
    2
    scriptBullet is correctly assigned to bullet. I have also tried scriptBulletPool.MyInstance.returnBullet(this.gameObject) and simply put inactive and active instead of add and remove calls, but i didn't work.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Try adding Debug.Log() to GetBullet and ReturnBullet that output the number of items in the List. That way you can check if they are correctly (as expected) returned, and if you are draining them from the pool as well.