Search Unity

Object Pooling

Discussion in 'Physics' started by truthseeker089, Jun 22, 2019.

  1. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    hi guys I'm trying to instantiate a bullet using Object Pooling, I am new to programming games and I don't know why no bullet is being instantiated if I press fire2 although there are pre instantiated bullet clones in my Game Controller...thanks!

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

    public class bulletspawn : MonoBehaviour {
    public GameObject bullet;
    public List<GameObject> bulletspawner; //
    public List<GameObject> activePlayerTurrets;
    public float scatterShotTurretReloadTime = 2.0f;

    void Start()
    {

    }

    void FixedUpdate()
    {
    bool shoot = Input.GetButtonDown ("Fire2");

    foreach(GameObject turret in activePlayerTurrets) {
    GameObject bullet = objectpooler.SharedInstance.GetPooledObject ();
    if (bullet != null) {
    bullet.transform.position = turret.transform.position;
    bullet.transform.rotation = turret.transform.rotation;
    bullet.SetActive (true);
    }
    }
    }
    IEnumerator Activatebulletspawner ()
    {

    while (true) {
    foreach (GameObject turret in bulletspawner) {
    GameObject bullet = objectpooler.SharedInstance.GetPooledObject ();
    if (bullet != null) {
    bullet.transform.position = turret.transform.position;
    bullet.transform.rotation = turret.transform.rotation;
    bullet.SetActive (true);
    }
    }

    yield return new WaitForSeconds (scatterShotTurretReloadTime);

    {

    }
    }
    }
    }
     
  2. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    I got the script in a nice tutorial I just tried to combine it with my script so its a bit convoluted, the turret is not supposed to be there also the activePlayerTurrets I just don't know how to replace it