Hey guys ive been trying to create a pool for my obstacles that spawn in front of the player in an endless runner. So i tried creating a simple pooling system so its not constantly instantiating and destroying, but ive kinda hit the road block with it.
atm im using a simple click to activate and right click to deactivate to test,
but when i right click it deactivates all the objects but doesnt add them all to the inactive list correctly. for example sometimes it adds only 1 of the objects 3 times.
This goes on the object,
public class pooledObj : MonoBehaviour
{
void Update ()
{
if(Input.GetMouseButtonDown(1))
{
Deactivate();
}
}
void Deactivate()
{
gameObject.SetActive(false);
transform.position = Vector3.zero;
objPool.objPoolSelf.DeactivateObject();
}
}
This goes on a manager gameobject
public class objPool : MonoBehaviour
{
public List<GameObject> prefabs = new List<GameObject>();
public List<GameObject> prefabPoolInactive = new List<GameObject>();
public List<GameObject> prefabPoolActive = new List<GameObject>();
public GameObject tempPool;
public static objPool objPoolSelf;
//public int numberOfPrefabs = 0;
void Awake()
{
objPoolSelf = this;
}
// Use this for initialization
void Start ()
{
for (int i = 0; i < prefabs.Count; i++)
{
prefabPoolInactive.Add((GameObject)Instantiate(prefabs*));*
_ prefabPoolInactive*.SetActive(false);_
_ }_
_ }_
_ void ActivateObject()_
_ {_
_ for (int i = 0; i < prefabs.Count; i++)_
_ {_
_ if (prefabPoolInactive.active == false)
{
tempPool = prefabPoolInactive[Random.Range(0, prefabPoolInactive.Count)];
tempPool.SetActive(true);
prefabPoolActive.Add(tempPool);
prefabPoolInactive.Remove(tempPool);
return;*_
* }*
* }*
* }*
* public void DeactivateObject()*
* {*
* prefabPoolInactive.Add(tempPool);*
* prefabPoolActive.Remove(tempPool);*
* }*
* // Update is called once per frame*
* void Update ()*
* {*
* if(Input.GetMouseButtonDown(0))*
* {*
* ActivateObject();*
* }*
* }*
}
Is it also possible to add more than 1 of each prefab the list when instantiated so i have more than 1 of the object to activate?
So eventually what i want to do is pick a random object from the inactive list place it infront of the player and put it in the active list, once its behind the player, deactivate it and put it back in the inactive list, pretty much rinse and repeat. The main reason i started doing this was because i wanted to randomly pick one from the list of objects most of the other pooling examples ive seen only has 1 object and cant pick randomly from it.
Anyway any help would be appreciated and thanks for reading.
PS im very new to programming this is as far as i got without help.