- Home /
Question by
Cross_J · Nov 09, 2020 at 05:34 AM ·
countobject pool
How can I keep a count of my active objects from my object pool?
I'm coming back to making some games after a long break and my brain is all mush so i need some help on this one.
I'm wanting to keep track of how many objects from my Object Pool are active in game and then be able to turn them all off if it exceed x number, so basically to be used as a bullet and retrieving a bullet is the use case. Right now I'm trying to count by the code below:
public class GameController : MonoBehaviour
{
int activeBulletCounter;
private void Awake()
{
activeBulletCounter = 0;
}
void Update()
{
countActiveBullet();
Debug.Log(activeBulletCounter);
}
void countActiveBullet()
{
GameObject[] BulletArray = GameObject.FindGameObjectsWithTag("Bullet");
foreach (GameObject bullet in BulletArray)
{
if (bullet.activeSelf && bullet.GetComponent<BulletController>().usedUp)
{
activeBulletCounter++;
}
}
}
}
Obviously this won't work as the counter would continuously count each frame it sees an bullet that's active and usedUp = true with out keeping track of which bullet.
What would be the best way to work around this?
Thanks.
Comment