Quantcast
Channel: Answers for "Dynamic AddComponent?"
Viewing all articles
Browse latest Browse all 6

Answer by cjdev

$
0
0
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); }

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>