Search Unity

Resolved Fix Enemy fire two bullets shot at once with Object Pooling

Discussion in 'Scripting' started by bamoibam, May 30, 2022.

  1. bamoibam

    bamoibam

    Joined:
    Jul 5, 2020
    Posts:
    2
    I used pooling to reproduce bullets coming out of the enemy, however, when it firing, it always fires 2 bullets at the same time.
    Any help will be appreciated, thank you.

    Enemy.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         StartCoroutine(Fire(bulletRate));
    11.     }
    12.     private void Bullet()
    13.     {
    14.         //GameObject bullet = GameObject.Instantiate(bulletPrefab, transform.position, transform.rotation) as GameObject;
    15.         GameObject bullet = ObjectPool.instance.GetPooledObject(); //using Object Pool
    16.         if (bullet != null)
    17.         {
    18.             bullet.transform.position = transform.position;
    19.             bullet.transform.rotation = transform.rotation;
    20.             bullet.SetActive(true);
    21.         }
    22.     }
    23.  
    24.     private IEnumerator Fire(float spawnR)
    25.     {
    26.         while (this.gameObject != null)
    27.         {
    28.             Bullet();
    29.             yield return new WaitForSeconds(spawnR);
    30.         }
    31.     }
    32. }
    33.  
    and here is my ObjectPool.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ObjectPool : MonoBehaviour
    6. {
    7.     public static ObjectPool instance;
    8.     public GameObject objectToPool;
    9.     public List<GameObject> pooledObject;
    10.     public int amount;
    11.     // Start is called before the first frame update
    12.     void Awake()
    13.     {
    14.         CheckIfObjectPoolIsInTheScene();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Start()
    19.     {
    20.         pooledObject = new List<GameObject>();
    21.         for (int i = 0; i < amount; i++)
    22.         {
    23.             GameObject temp = (GameObject)Instantiate(objectToPool);
    24.             temp.transform.SetParent(this.transform);
    25.             temp.SetActive(false);
    26.             pooledObject.Add(temp);
    27.         }  
    28.     }
    29.  
    30.     public GameObject GetPooledObject()
    31.     {
    32.         for (int i = 0; i < amount; i++)
    33.         {
    34.             if(!pooledObject[i].activeInHierarchy)
    35.             {
    36.                 return pooledObject[i];
    37.             }
    38.         }
    39.         return null;
    40.     }
    41.  
    42.     private void CheckIfObjectPoolIsInTheScene()
    43.     {
    44.         if (instance == null)
    45.         {
    46.             instance = this;
    47.         }
    48.         else
    49.         {
    50.             Destroy(this.gameObject);
    51.         }
    52.     }
    53. }
    54.  
    Each time Enemy shot, it shot 2 bullets at the same time
    upload_2022-5-30_13-29-59.png
    and the 2nd bullet is always in the same position as the previous bullet
    upload_2022-5-30_13-30-16.png
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Nothing seems wrong with the code. Do you have two Enemy scripts on your object?
     
  3. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    Usual debugging techniques apply:
    • Add some debug log statements, check if code actually gets executed twice and which parts
    • If code really gets executed twice, check the callstack. Find out from where it's called. Is one of the two calls coming from somewhere unexpected.
    • If the origin of all calls are coming from scene objects use the objects associated with log calls to find out which object instances is the script associated with. Maybe there are more copies of it in the scene than you expected.
     
  4. bamoibam

    bamoibam

    Joined:
    Jul 5, 2020
    Posts:
    2
    Somehow it worked as it should be now. I really don't understand.
    Before whatever I do, the enemy still shoots 2 bullets at once (change bullet prefab, use InvokeRepeat instead of Coroutines, check if any code is executed twice,...etc)
     
  5. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    mightve been lag. like how Debug version of software runs way slower than Release versions. if you have the time, build the game and check within the app, not unity game