Search code examples
c#unity-game-enginec#-4.0object-pooling

How to set Object pooling correctly in 2D game?


I have a couple of problems with object pooling in Unity with my 2D game, cannon balls don't want to stop when there is a collision with the wall, and 1 of them bursts shoot and the other 9 shoot together connected with each other, my cannon is static object on scene. Can someone help or give me some hint about it.

Here is my code, 3 scripts:

ObjectPooling.cs

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

public class ObjectPooling : MonoBehaviour
{

    public static ObjectPooling instance;
    [SerializeField] public GameObject objectToPool;
    private List<GameObject> cannonBalls = new List<GameObject>();
    private int numberOfObjects = 20;


    private void Awake()
    {
        instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < numberOfObjects; i++)
        {
            GameObject gameObject = Instantiate(objectToPool);
            gameObject.SetActive(false);
            cannonBalls.Add(gameObject);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }


    public GameObject GetCannonBallObject()
    {
        for (int i = 0; i < cannonBalls.Count; i++)
        {
            if (!cannonBalls[i].activeInHierarchy)
            {
                return cannonBalls[i];
            }
        }
        return null;
    }

}

Cannon.cs

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

public class Cannon : MonoBehaviour
{
    
        [SerializeField] private Rigidbody2D rb;
        [SerializeField] private GameObject cannonBall;
        [SerializeField] private Transform cannonBallPosition;
        void Start()
        {

        }

        private void Update()
        {
            Fire();
        }

        private void Fire()
        {
            cannonBall = ObjectPooling.instance.GetCannonBallObject();

            if(cannonBall != null)
            {
            cannonBall.transform.position = cannonBallPosition.position;
            cannonBall.SetActive(true);
            }

        }

    }

CannonBall.cs

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

public class CannonBall : MonoBehaviour
{

    private float speed = 10f;
    [SerializeField] private Rigidbody2D rb;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        rb.velocity = Vector2.left * speed;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("FloorAndWall"))
        {
          // Destroy(this.gameObject);
            gameObject.SetActive(false);
        }
    }
}

Solution

  • Why is you cannonball static? Have your tried not having it marked as static? Also, this problem has nothing to do with object pooling. Make sure that when your objects are enabled, all the components are active. Finally, when working with rigidbodies, you should handle them inside FixedUpdate(), and not inside Update().