This is how I do it, and I know for sure that it will compile in Unity3d. OfType is not in Unity, by the way, TypeOf is just like taras.roshko, if you use is better.
MonoBehavior[] list; foreach(MonoBehavior current in list) // Search the whole list for MonoBehaviors. if (current is Alpha) // Check if current is an Alpha type. current.doSomething(); // current is a valid Alpha type script.
foreach will try to convert all the elements to a list, and since Alpha is MonoBehavior, but MonoBehavior is not Alpha, you cannot start it correctly. This way you check if the current alpha is, and then if it uses it as you like.
user2085599
source share