This question will seem similar to some that have been asked before, but bear with me.
Basically, I want to instantiate a variable at runtime using nothing but a string that represents the type the variable needs to be. Pretty much every existing question gave me something like this:
Type type = Type.GetType(stringrepresentingType);
var newObject = Activator.CreateInstance(type);
This works, but as it stands, newObject is type object. I want to be able to convert the object created by the Activator into the type stored in type, so it would look like this:
Type type = Type.GetType(stringRepresentingType);
var newObject = (type)Activator.CreateInstance(type);
This throws errors due to type being a variable. I've also tried typeof(type) and (Type.GetType(stringRepresentingType) before the Activator, and neither work.
How can I convert the newObject variable without using an if/else?
Edit:
I'll be using newObject in logical comparisons (such as == or ||), hence the reason I can't leave it as type object. I know I can just use an if/else to check the type after the fact, but I was curious if there was a more elegant solution. I'd use dynamic as opposed to var, but unfortunately Unity3D does not support it.