- Home /
GameObject Pool heaving difficulties getting a pool at all trying to find different aproach
any small pushes how should I approach this will be helpful thanks in advance
problem is not within the code thing is I wanted to go not to destroy and istantiate a new GO at all
but as soon as I moved to figureing what kind of GO are actually falsed I figured out I'm already destroying a GO so doing all complex for nothing
problem is the first one might never get falsed and the last one will or the other way around
this are not bullets but bricks or what ever that are false ONLY when player picks them up
yes this works smoothly and all but I think it's even less effective than just destroying a GO and instantiating new one
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// TRAINING PURPOSES ONLY
public class Pooling : MonoBehaviour {
public GameObject prefab ;
public List<GameObject> ActiveCopy ;
public List<GameObject> DeactiveCopy ;
public float time = 0;
public float time1 = 0;
public float timeremaining = 0.1f ;
public bool NewOne = false ;
void Update (){
if (! NewOne){
time = Time.time ;
NewOne = true ;
}
time1 = Time.time ;
if ((time1 - time) > timeremaining){
ActiveCopy.Add(prefab);
ActiveCopy[ActiveCopy.Count-1] = Instantiate( ActiveCopy[ActiveCopy.Count-1] ) as GameObject;
ActiveCopy[ActiveCopy.Count-1].transform.position = transform.position ;
ActiveCopy[ActiveCopy.Count-1].transform.parent = transform ;
Deactivate(ActiveCopy.Count-1);
NewOne = false ;
}
}
void Deactivate (int Count) {
ActiveCopy[Count].SetActive(false) ;
DeactiveCopy.Add(ActiveCopy[Count]) ;
ActiveCopy.Remove(ActiveCopy[Count]) ;
}
}
I didn't create with
public GameObject[] Copy
because every time I do than
Copy = new GameObject[(IncreasedNumber)]
all GO are simply deleted within Copy and Copy does not contain them any longer while they exist in scene and I can't track them
I thinked a bit more and heard some times about taggs I don't even know how does tagging work
all I want is find the object that's false it doesn't matter first false or last false one but defenally I don't want to pick up the true one
they will not get false over time but randomely by player when player picks it up
If I'm reading your code correctly it looks like your are setting every object to SetActive(false) right when they are instantiated. Is there a reason for this? $$anonymous$$aybe I don't understand what you are trying to accomplish.
am yes I'm just setting them to false right away I didn't want to bother with 20 more lines of code for learning purposies
but the thing is when I want to create them false can change in future but the problem is I'm looking from wrong angle as I need to destroy/create new GO again
just to make sure all destroyed are in 1 place so I don't need to search for them when creating later
(yeah didn't even went that far with code because I'm at wrong angle already) and searching through web for solution
I was thinking somehow tagging but I have no knowledge about tagging
I could get in a few ifs for that function or when that functon is called (when player clicks on object) but all that is already done and don't need to focus there
I'm focusing not to making a garbage collection work too much
I think there may be a bit lost in translation here. When you say "destroyed" you mean you've SetActive(false)? Not actually destroyed the GO but simply deactivated it? And when you reference an index in DeactiveCopy like: DeactiveCopy[index] it returns a NULL value?
yes when I give ActiveCopy.Remove(ActiveCopy[Count]) ;
is same as I'm doing destroy GO isn't it? (not as a code GO still exists but garbage collector)
and when I got there I said Oh I actually achieved nothing
this script is learning purpose how not to destroy a GO but heaving a pool
atm I'm destroying GO with other scripts not this one
I was thinking giving it to different array so I could easily find falsed ones and when I saw what I've done I saw I need different aproach and for this aproach I'm asking you guys I don't have a clue
Ah so you don't want to have to use DeactiveCopy at all then? So you want one array with "Active" and "Deactive" GameObjects but be able to deter$$anonymous$$e which ones within are active and which ones are not?
Answer by theundergod · Feb 24, 2013 at 01:25 AM
First instantiate your objects and put them in the list however you'd like.
Example:
List<GameObject> gameObjects = new List<GameObject>();
//instantiate 20 GameObjects and set every other one to inactive
for(int x = 0; x < 20; x++)
{
//add an instantiated GameObject
gameObjects.Add((GameObject)Instantiate (prefab, prefab.transform.position, prefab.transform.rotation));
//if a round number then set that object to inactive
if(x%2 == 0)
{
gameObjects[x].active = false;
}
}
There are a couple ways to find the inactive objects
1 - Loop through and check the Active state.
foreach(GameObject go in gameObjects)
{
if(go.active)
{
//do something with the active GameObject
}
else
{
//do something with the inactive GameObject
}
}
2- Using Linq in C#
IEnumerable<GameObject> inactiveGameObjects = System.Linq.Enumerable.Where(gameObjects, n => n.active == false);
foreach(GameObject go in inactiveGameObjects)
{
Debug.Log(go.name);
}
3- Using Tags in Unity Set the GameObjects tag by using
yourObject.tag = "TagName";
Then get all GameObjects with tag "TagName" using
GameObject[] inactiveGameObjects = GameObject.FindGameObjectsWithTag("TagName");
Be sure to add the Tag in the Tag/Layer editor in Unity or Unity won't recognize it as a valid tag.
I'll update in a second with more concise code if you'd like.
am are 1 and 2. separate and not 1 for another if so
can you explain more about 2. one?
I didn't have in $$anonymous$$d to loop through all objects
There are many ways to accomplish this but if you'd like to learn more about Linq check out this reference: http://msdn.microsoft.com/en-us/library/bb397933.aspx
In order to understand the
n => n.active = false
I would recommend looking at http://msdn.microsoft.com/en-us/library/bb397687.aspx
Lambda expressions can be very powerful although I think we may be getting way out of scope of your particular exercise.
Ah I was going a little overboard then. If you definitely want to use tags you can simply set the tag using
yourObject.tag = "TagName";
Then use
GameObject[] inactiveGameObjects = GameObject.FindGameObjectsWithTag("TagName");
Then cycle through the inactiveGameObjects array.