You can do it but it's not pretty, you need to use the types and reflection like this:
List componentList = new List();
componentList.Add(typeof(Rigidbody));
componentList.Add(typeof(Animator));
GameObject go = new GameObject("MyGameObject");
foreach(Type component in componentList)
{
var methodInfo = typeof(GameObject).GetMethods().Where(x => x.IsGenericMethod)
.Where(x => x.Name == "AddComponent").Single();
var addComponentRef = methodInfo.MakeGenericMethod(component);
addComponentRef.Invoke(go, null);
}
↧