Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Attempt at Object Pooling failing miserable, works about 10 times then suddenly quits without errors

Discussion in 'Scripting' started by LordBlackwood, Oct 20, 2015.

  1. LordBlackwood

    LordBlackwood

    Joined:
    Aug 10, 2013
    Posts:
    26
    Hey guys, I have an enemy firing bullets. I'm trying to use object pooling on this bullets and it's just not behaving the way I expect it to. It works just fine nearly 10 times, but then halfway through the 10th firing the bullets deactivate halfway to the target, and then from there on out activate and deactivate almost instantly in the enemy's muzzle. Here's the relevant code:

    On the projectile object:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class BaseProjectileScript : MonoBehaviour
    5. {
    6.  
    7.     [SerializeField]
    8.     private float speed = 10f;
    9.     [SerializeField]
    10.     private float range = 40f;
    11.     [SerializeField]
    12.     int damageDealt = 5;
    13.     [SerializeField]
    14.     DamageType damageType = DamageType.Normal;
    15.  
    16.     //object pooling
    17.     private float distance;
    18.  
    19.     //read only properties
    20.     public float Speed
    21.     {
    22.         get
    23.         {
    24.             return speed;
    25.         }
    26.     }
    27.     public int DamageDealt
    28.     {
    29.         get
    30.         {
    31.             return damageDealt;
    32.         }
    33.     }
    34.     public DamageType DamageType
    35.     {
    36.         get
    37.         {
    38.             return damageType;
    39.         }
    40.     }
    41.  
    42.     // Update is called once per frame
    43.     void Update()
    44.     {
    45.         transform.Translate(Vector3.right * Time.deltaTime * speed);
    46.         distance += Time.deltaTime * speed;
    47.         if (distance >= range)
    48.         {
    49.             gameObject.SetActive(false);
    50.         }
    51.     }
    52.  
    53.     void OnTriggerEnter(Collider other)
    54.     {
    55.         if (other.tag == "player" || other.tag == "engine")
    56.         {
    57.             UnitBaseScript unit = other.GetComponentInParent<UnitBaseScript>();
    58.             unit.ApplyDamage(damageDealt, damageType);
    59.             gameObject.SetActive(false);
    60.         }
    61.     }
    62. }
    On the enemy unit:
    Code (CSharp):
    1. void FireProjectile()
    2.     {
    3.         //TODO: Add audio
    4.         //audio.Play();
    5.  
    6.         //set the next fire time to be after a set reload time
    7.         nextFireTime = Time.time + reloadTime;
    8.  
    9.         //set the next movement time to be after a pause during the shot
    10.         nextMoveTime = Time.time + firePauseTime;
    11.  
    12.         //calculate just how much error the next shot is going to have
    13.         CalculateAimError();
    14.  
    15.         //instantiate the projectiles and muzzle flashes
    16.         foreach (Transform muzzleFlash in muzzleFlashes)
    17.         {
    18.             float size = UnityEngine.Random.Range(0.9f, 1.1f);
    19.             muzzleFlash.localScale = new Vector3(size, size, size);
    20.             muzzleFlash.gameObject.SetActive(true);
    21.             StartCoroutine(DeactivateMuzzleFlash(muzzleFlash));
    22.             ;
    23.         }
    24.  
    25.         foreach (Transform muzzlePosition in muzzlePositions)
    26.         {
    27.             for (int i = 0; i < projectiles.Count; i++)
    28.             {
    29.                 if (projectiles[i].activeInHierarchy == false)
    30.                 {
    31.                     projectiles[i].transform.position = muzzlePosition.transform.position;
    32.                     projectiles[i].transform.rotation = muzzlePosition.transform.rotation;
    33.                     projectiles[i].SetActive(true);
    34.                     break;
    35.                 }
    36.                 else
    37.                 {
    38.                     Debug.Log("pew");
    39.                     continue;
    40.                 }
    41.             }
    42.  
    43.         }
    44.     }
     
  2. LordBlackwood

    LordBlackwood

    Joined:
    Aug 10, 2013
    Posts:
    26
    Oh...oh god, I'm an idiot. It was my distance variable. Of course...I wasn't resetting it so it was always instantly deactivating after a certain point. Sorry for wasting anybody's time.