Question by
rmail2006 · May 03, 2020 at 08:42 PM ·
typefindobjectoftype
How can I gather a list of GameObjects from List of Types
Hi guys,
So I've got a list of Types in my script
public List<Type> InteractableTypes = new List<Type>();
and for each of the types, I'm wanting to find all gameobjects that have that component attached and store them in a list.
Problem is, I'm not sure on how to get this to work with GameObject.FindObjectsOfType<>();
public List<Type> InteractableTypes = new List<Type>()
{
typeof(Tree),
typeof(Rock),
typeof(Mine)
};
public Dictionary<Type, GameObject[]> ListOfObjects = new Dictionary<Type, GameObject[]>();
public void GatherAllReferences()
{
ListOfObjects.Clear();
foreach (Type type in InteractableTypes)
{
GameObject[] o = FindObjectsOfType(type) as GameObject[];
ListOfObjects.Add(type, o);
}
}
Does anyone know how I can pass in a Type reference to find all components or figure out what else I may have missed :)
Comment