Casting FindGameObjectsByTag to another type
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;
}
What is Agent, and where is it found? Is it a component on the game object, or is it a game object?
Beyond that, you a very likely to discover that any of the find methods are too slow to call during every update. I sense you need a new strategy that doesn't impact performance so badly.
Finally, there's no reason a $$anonymous$$DTree structure can't be fashioned to handle the types you require, what have you attempted or researched that makes you believe it can't accept the Agent?
I'm agree with JVene. You shouldn't use such heavy and slow methods, especially in Update.
However, if 'Agent' is a component attached to game objects, you would search for GameObjects as you did before and then check if they have an Agent attached to them:
foreach (GameObject go in allObjects)
if (go.GetComponent<Agent> () && go.tag == tag)
data.Add (go.GetComponent<Agent> ());
Answer by nico_rawitscher · Aug 26, 2018 at 12:24 PM
@JVene, sorry for not clarifying that part, Agent
is a class attached to a prefab. The KDTree class does not accept a type of GameObject
because its is not a valid constraint for its Generic type T because its not an interface or a sealed class. So it only accepts a type Component
for its type constraint, This is the reason I have to use my type Agent in the KDTree.
public class KdTree<T> : IEnumerable<T>, IEnumerable
where T : Component
@Ermiq actually your small fix was the proper optimized solution. Thank you, here is the final bit of code.
private List<Agent> FindObjectsWithTag(string tag)
{
List<Agent> data = new List<Agent>();
GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
foreach (GameObject item in allObjects)
if (item.GetComponent<Agent>() && item.tag == tag)
data.Add(item.GetComponent<Agent>());
return data;
}
Your answer
Follow this Question
Related Questions
Get Childrens to new array from perents array. 1 Answer
GameObject[] doesn't have a definition for transform/name/etc... 1 Answer
When 0 elements with a tag, get a value of 13 1 Answer
Why can't I find a GameObject with FindGameObjectsWithTag 2 Answers
Inheritance and type casting problems 0 Answers