I would very much like to know what is your usecase here?
You can something like Type.GetType
using the full reference name like e.g. "UnityEngine.Rigidbody"
but I'm not sure if this is what you would really want to do
var obj = new GameObject();
foreach (var cp in cpList)
{
// Maybe try full names like e.g. "UnityEngine.Rigidbody"
obj.AddComponent(Type.GetType(cp));
}
this is very error prone and not very efficient.
Alternatively also using Reflection and Assembly.GetType
but then you need to know at least one type in the same assembly:
Assembly asm = typeof(UnityEngine.Object).Assembly;
Type type = asm.GetType("UnityEngine.Rigidbody");
You could rather use some hardcoded dictionary like e.g.
public static readonly Dictionary<string, Type> componentTypes = new Dictionary<string, Type>()
{
// some examples
{"Camera", typeof(Camera)},
{"SomeOtherScript", typeof(SomeOtherScript)}
}
and then do something like
var obj = new GameObject();
foreach (var cp in cpList)
{
if(!componentTypes.ContainsKey(cp))
{
Debug.LogError($"For the name {cp} no type was defined!");
}
obj.AddComponent(componentTypes[cp]);
}
Same could be done also with a simple switch - case
.
Trade-off ofcourse you need one line/entry for each component you are going to tackle.
Note: Typed on Smartphone so no warranty but I hope the idea gets clear.