- Home /
Help Please How to make the pooled object (bullet) disable after 10 seconds
I've been making a multiplayer top-down tank game, and I'm having a problem to make a script for this situation. i have a script of object pooling to my bullet so we can save memory, and we need to disable it after 10 seconds after firing. And also we want to limit the bullet to 5, so if the bullet is less than 5, you can still fire, if its already 5, you will not be able to fire (unless your bullet count became less than 5).`
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ObjectPool : MonoBehaviour { [SerializeField] protected GameObject objectToPool; [SerializeField] protected int poolSize = 10;
protected Queue<GameObject> objectPool;
public Transform spawnedObjectsParent;
private void Awake()
{
objectPool = new Queue<GameObject>();
}
public void Initialize(GameObject objectToPool, int poolSize = 10)
{
this.objectToPool = objectToPool;
this.poolSize = poolSize;
}
public GameObject CreateObject()
{
CreateObjectParentIfNeeded();
GameObject spawnedObject = null;
if (objectPool.Count < poolSize)
{
spawnedObject = Instantiate(objectToPool, transform.position, Quaternion.identity);
spawnedObject.name = transform.root.name + "_" + objectToPool.name + "_" + objectPool.Count;
spawnedObject.transform.SetParent(spawnedObjectsParent);
spawnedObject.AddComponent<DestroyIfDisabled>();
}
else
{
spawnedObject = objectPool.Dequeue();
spawnedObject.transform.position = transform.position;
spawnedObject.transform.rotation = Quaternion.identity;
spawnedObject.SetActive(true);
StartCoroutine(DeactivateBullet());
}
objectPool.Enqueue(spawnedObject);
return spawnedObject;
}
private void CreateObjectParentIfNeeded()
{
if (spawnedObjectsParent == null)
{
string name = "ObjectPool_" + objectToPool.name;
var parentObject = GameObject.Find(name);
if (parentObject != null)
spawnedObjectsParent = parentObject.transform;
else
{
spawnedObjectsParent = new GameObject(name).transform;
}
}
}
private void OnDestroy()
{
foreach (var item in objectPool)
{
if (item == null)
continue;
else if (item.activeSelf == false)
Destroy(item);
else
item.GetComponent<DestroyIfDisabled>().SelfDestructionEnabled = true;
}
}
private IEnumerator DeactivateBullet()
{
yield return new WaitForSeconds(10f);
spawnedObject.SetActive(false);
}
}`
@MilitaryG @Bunny83 hi bunny i saw your an expert hehe could you help me? Hi i dont know what you mean, im a bit new to this, so what i mean or if you want preference is like the old game named az tank or tank trouble which the limit of bullet is 5 and cannot fire if its 5, if its less than 5 bullets(fired) you can fire again, hence, the object pool, i remember when the first pooling is it disable after 10 seconds, i have 10 seconds timer , (in another script) kinda bit messy, but when its recycled it doesnt disable anymore after 10 seconds, and now even the first 5 bullets fired dont disable anymore,
for disabling the object you would need to change the code this
private IEnumerator DeactivateBullet(GameObject bullet)
{
yield return new WaitForSeconds(10f);
bullet.SetActive(false);
}
StartCoroutine(DeactivateBullet(spawnedObject));
but your script is still missing the full object pooling functionality, you should be saving the active bullets in a different list, so that you can return it to the bullets queue once it gets disabled, check this video to understand the concept behind object pooling
But where should i put it? I watched the video, i dont understand what you mean, i followed some and add it to my script after watching the video, the problem now is where should i put the deactivator or the deactivatebullet
I have the turret script, bullet script, and object pooling script,
All my script is kinda copied from others because im still new and dont understand much, hehe. @xxmariofer
just change your DeactivateBullet method with the DeactivateBullet I posted, and the startcoroutine of your method with the startcoroutine I posted
it works on the first 5 bullets but when i fired again it doesnt work , also we've been having problem called nullreferenceexception i don't know where it came. if you want i can give you details that you want