I have been having this issue with Unity.
Issue in short.
While having a large object pool, and a list that stores its state, List.Find is not able to Find the Object state even though the Object is there in the List.
Issue in Detail.
I have attached here a sample Unity Package of my issue. The Package has a Object Pool of 512 GameObjects and a Script that is placed on the parent of the Object Pool.
The Script is designed to Enable Render of the Objects that are in the Object Pool, Give them a name like Cube3, Cube6, Cube900, etc and also place them at a distance of 3 Units from each other in X axis.
The script does the above till count becomes 300. And the State of Cube is stored in the Class CubeState using a List.
The next part of script checks if Count is greater than 256 and if so, Find Cubes using the CubeState List and if it finds a cube with the given position and if its visible.
Once if it Finds the Cube, it turns off the renderer and changes its name to free and reduces the Count variable by 1 for each Cube.
Now the Issue is that there is no error in this Logic but there is something thats going wrong with respect to List.Find wherein it always return null, hence there is no new Cube with its name greater than Cube900.
Help find a way to solve Finding Objects from the List.
Link to the Package. - https://drive.google.com/file/d/0BzeclL_12cK-VW9hbTJPZTNjalk/edit?usp=sharing
Code Snippet
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SampleScript : MonoBehaviour
{
public int count = 0;
private List<CubeState> _cubeState = new List<CubeState>();
private GameObject _localGameObject;
private Vector3 _position = new Vector3(0,2,0);
private CubeState cs=new CubeState();
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (count < 300)
{
_localGameObject = GameObject.Find("free");
_position.x += 3;
_localGameObject.transform.position = _position;
_localGameObject.renderer.enabled = true;
_localGameObject.name = "Cube" + _position.x;
count++;
cs.position = _position;
cs.visible = true;
_cubeState.Add(cs);
}
if (count > 256)
{
for (int i = 0; i < _position.x ; i+=3)
{
if (FindCube(i)!=null&&FindCube(i).visible)
{
string _temp = "Cube" + i;
_localGameObject = GameObject.Find(_temp);
_localGameObject.transform.position = Vector3.zero;
_localGameObject.renderer.enabled = false;
_localGameObject.name = "free";
count--;
_cubeState.Remove(FindCube(i));
}
}
}
}
private CubeState FindCube(int i)
{
return _cubeState.Find(item=>item.position.x==i);
}
}
public class CubeState
{
public bool visible = false;
public Vector3 position = Vector3.zero;
}