Hi guys, I am trying to retrieve all GameObjects with a certain tag on the scene during every update call. GameObject.FindGameObjectsWithTag()
retrieves an [] of GameObjects
but Its no use for me because I have a specific class which is of type Agent
. I wrote my own “version” of what I thought that method could be, but Its not performing similar to GameObject.FindGameObjectsWithTag()
method, It doesn’t seem to retrieve properly the agents . I have a previous version of the project where I don’t implement Agent
type specifically and instead use GameObject
and everything is fine. But know I have to implement a KDTree data structure to optimize my script and it does not accept GameObject
as type.
Any tips / help would be great! thanks
This is my version of GameObject.FindGameObjectsWithTag()
private List<Agent> FindObjectsWithTag(string tag)
{
List<Agent> data = new List<Agent>();
Agent [] allObjects = UnityEngine.Object.FindObjectsOfType<Agent>();
foreach (Agent item in allObjects)
{
if (item.gameObject.tag == tag ) data.Add(item);
}
return data;
}