Hey guys thanks for reading my question.
I’m creating a generic pooling object i manage to spawn,store objects. But i want to have one clean empty pool object containing the instantiated objects, and make my other Objects to request object from there.
What’s the best way to do this? I was thinking on creat a function on my pool object that search the desire object and returns it but i don’t know how to write the function to be accessible from any object without using GetComponent every time, a public/static function or extending Monobehaviour maybe?
===== EDIT ======
Thanks for the answer Boredmormon, maybe i need to explain a little more, setactive or not it’s great that’s how i had dicided to manage the objects my only issue is pulling the objects from the pools, i’ll have multiple pools difined from the inspector.
My code for the pool so far:
#pragma strict
import System.Collections.Generic;
//CREAR PILETAS DINAMICAS =START=
public class ObjectsPool extends System.Object {
var Objeto : GameObject;
var Cantidad : int;
var Escalable : boolean;
var Pileta = new List.<GameObject>();
}
var GrupoDePiletas : ObjectsPool[];
function Start () {
for(var piletas : ObjectsPool in GrupoDePiletas){
for(var i = 0;i < piletas.Cantidad;i++){
var obj = Instantiate(piletas.Objeto,Vector3(0,0,0),Quaternion.identity);
obj.SetActive(false);
piletas.Pileta.Add(obj);
}
}
}
//CREAR PILETAS DINAMICAS =END=
function Update () {
}
I’ve got a blog post that offers one option over here.
The general idea is that you add another component to the pooled object (poolee?). That component contains a reference back to the object pool it came from, as well as the OnDisable method to add it back to the pool. That way you simply call SetActive(false) when you are done with the GameObject and it goes away automatically.
I would suggest against using a static function for returning to a pool. Typically you end up with more then one pool instance using the same code.
You could have all of your poolable GameObjects implement an interface or inherit from the same base class, but it gets messy quickly. In this case a wide inheritance structure is better then a deep one.
Read your edit. I still think the blog post is worth checking out. I use a custom property to get and return GameObjects. Then I remove the object from the list. There is another approach to object pooling that is to scan through the List and return one that is inactive. The tutorial [here][1] does it nicely. I chose to do the remove from list versus scan the list primarily because I do some modification to the inactive objects inside the pools. [1]: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
Well i decided to give up and just get reference to the pool script for now i don’t have so many spawn objects.
I leave the code for anyone how is working on pooling Objects as an alternative.
I create a list of individual pools with different GameObjects Quantities and Grow or not variables.
Just create an Object attach the script and then call LoadObject(IndexOfThePool) and use it.
//CREAR PILETAS DINAMICAS =START=
public class ObjectsPool extends System.Object {
var Objeto : GameObject; //Prefab or object to instantiate
var Cantidad : int; //Quantity
var Escalable : boolean; //Will Grow?
var Pileta = new List.<GameObject>(); //Actual list of Pooled objects
}
var GrupoDePiletas : ObjectsPool[];// Group of pools
//Init the pool with the specified quantities and objects
function Start () {
for(var piletas : ObjectsPool in GrupoDePiletas){
for(var i = 0;i < piletas.Cantidad;i++){
var obj = Instantiate(piletas.Objeto,Vector3(0,0,0),Quaternion.identity);
obj.SetActive(false);
piletas.Pileta.Add(obj);
}
}
}
//CREAR PILETAS DINAMICAS =END=
//Use this function to get an object from an specific pool
//For example Pool[0] Enemies , Pool[1] Bullets , Pool[2] Clouds...
function LoadObject(PoolNumber : int) {
for(var i = 0;i < GrupoDePiletas[PoolNumber].Pileta.Count;i++){
if(!GrupoDePiletas[PoolNumber].Pileta*.activeInHierarchy){*
Read your edit. I still think the blog post is worth checking out. I use a custom property to get and return GameObjects. Then I remove the object from the list. There is another approach to object pooling that is to scan through the List and return one that is inactive. The tutorial [here][1] does it nicely. I chose to do the remove from list versus scan the list primarily because I do some modification to the inactive objects inside the pools. [1]: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
– Kiwasi