If I were coding in generic C# I can create a list that only stores a certain type of object. If I accidentally try to add anything other than that type to the list I will be promptly told and can fix the problem quickly.
List<Foo> my_list = new List<Foo>(); my_list.Add(Bar) //Error
However because I'm using Unity my script needs to be attached to a GameObject, and that needs to be stored in the List.
List<GameObject> my_list = new List<GameObject>(); GameObject Foo = new GameObject; Foo.AddComponent<Foo>(); GameObject Bar = new GameObject; Bar.AddComponent<Bar>(); my_list.Add(Foo) //Fine my_list.Add(Bar) //Works, but I don't want it to
I only want the list to store "Foo" GameObjects (GameObjects with the Foo component), however there is nothing to stop me accidentally storing any type of GameObject, which could (and already has) caused me hours scratching my head trying to debug the problem.
Is there a way to subtype a GameObject so that it has the effect of
List<GameObject(Foo)> my_list? Is there a way to do it with arrays too?