- Home /
casting string as type(for passing to method calling for type)?
public string LocalStateComponentNameToActivate;
Managers.Game.SetState(typeof(LocalSceneStateActivator));
using the above 2 lines, which are taken from a larger script in C#, I'm attempting to pass to one of my GameManager scripts, the value of the string(that I'm entering in the editor, or through code) to that method(SetState), that method however calls for a type, or it throws errors everytime.
I've tried all the C# tricks I know of(changeType, etc), but I cant force the method(SetState) to take the string(WHICH IS THE LITERAL NAME OF THE TYPE I WANT TO PASS) and pass it as a type, no matter what means of casting/conversion I try.
I would greatly appreciate it if anyone can offer any insight on how to take a public string that I can edit/enter in the editor(that is the literal name of an existing Type I want to pass that already exists publicly), and pass that string to a method which calls for a type.
Thanks for any assistance.
Answer by DaveA · Jan 28, 2011 at 04:10 PM
time to explore the link, thanks for sharing a direction I can research :-)
thanks for the link, I found a solution from a read through of that link. just in case someone also runs into this issue, here's some code that worked for me:
/ private Type StringToTypeConverted = Type.GetType("StringNameOfTypeToBeConverted"); /