I am working on an Editor Function to get all GameObjects in the current scene and then filter them out based on the component given.
So far this is what I have in my Function::
public void ListBuild(List<int> m_pList, System.Type m_Component)
{
GameObject[] _Temp = Object.FindObjectsOfType(typeof(GameObject)) as GameObject[];
m_pList = new List<int>();
foreach (GameObject g in _Temp)
{
if (g.GetComponent(m_Component) != null)
m_pList.Add(g.GetInstanceID());
}
}
I am passing through a List I have declared at the top of my code which looks like this::
ListBuild(_LightCompsList, typeof(Light));
But when this list that is passed through goes through the foreach loop it doesn’t do what I want it to do.
Although if i replace the m_pList.Add with _LightCompsList.Add it works fine.
I am getting no errors at all.
Does anyone know how I can build this list by putting it into my function? I will have a lot of lists to build so this is needed.
You’re creating a new list with the same name as the one you get as a parameter. You’re not actually doing anything with the list sent into the function.
Your function would work as is, if you just remove this line:
m_pList = new List<int>();
Of course, if the list you send in is null, then it’ll fail, but that’s better than what’s happening now, which is that you’re creaing a new list and then not doing anything with it. You could check that the argument is not null as the first thing in the function.
Your whole function could also just be replaced with a Linq query using some generics: