I'm having a problem pooling some objects. I've looked at a starter file we got in class to recreate the scene but different, but I'm not having the correct results and I can't seem to figure it out :/
What I have is:
Environment which has 3 child object pools (ground, dew, corona). Ground has 3 childs (planes), dew and corona both have 10 childs (models).
What I'm trying to do is move the first object in an array to the last place of the array and changing the z position to move it forward once the player has ran past it.
My ground objects correctly go forward once my character has passed a certain position, the dews and coronas however do not, and I don't understand why.
This is the code I'm using:
public class Environment : MonoBehaviour
{
List<Transform> dews = new List<Transform>();
List<Transform> coronas = new List<Transform>();
List<Transform> grounds = new List<Transform>();
Transform player;
int propsCount = 0;
int groundCount = 0;
// Use this for initialization
void Start()
{
player = GameObject.Find("Player").transform;
dews = GameObject.Find("DewPool").GetComponentsInChildren<Transform>().ToList();
coronas = GameObject.Find("CoronaPool").GetComponentsInChildren<Transform>().ToList();
grounds = GameObject.Find("GroundPool").GetComponentsInChildren<Transform>().ToList();
dews.RemoveAt (0);
coronas.RemoveAt (0);
grounds.RemoveAt (0);
}
// Update is called once per frame
void Update()
{
// Voeg hier code toe voor Object Pooling!
Vector3 nieuwePos;
if (player.transform.position.z > dews[0].transform.position.z + 5)
{
dews[0].transform.position = nieuwePos = new Vector3(dews[0].transform.position.x, dews[0].transform.position.y, dews[0].transform.position.z + 50);
dews.Add(dews[0]);
dews.RemoveAt(0);
}
if (player.transform.position.z > coronas[0].transform.position.z + 5)
{
coronas[0].transform.position = nieuwePos = new Vector3(coronas[0].transform.position.x, coronas[0].transform.position.y, coronas[0].transform.position.z + 55);
coronas.Add(coronas[0]);
coronas.RemoveAt(0);
}
if (player.transform.position.z > grounds[0].transform.position.z + 40)
{
grounds[0].transform.position = nieuwePos = new Vector3(grounds[0].transform.position.x, grounds[0].transform.position.y, grounds[0].transform.position.z + 120);
grounds.Add(grounds[0]);
grounds.RemoveAt(0);
}
}
}
All the pools containing the objects are positioned at 0, the dews and coronas start at Z-index 7.5 and 10 respectively and the copies are always 5 Z further.
Player is an empty gameobject positioned at 0 - 0 - 4.4 containing the model Ethan.
The first dew and corona correctly get pushed backwards, the rest is very buggy.
If anyone can help out it would be greatly appreciated, thanks in advance!
Fixed it by adding
for (int i = dews.Count-1; i > 0; i-=2) {
dews.RemoveAt (i);
}
for (int i = coronas.Count-1; i > 0; i-=2) {
coronas.RemoveAt (i);
}
into my start() function, my objects had another child while my ground objects did not. Probably not the best solution but for now I'm okay with this :-)