- Home /
Convert String To Class
I have a String CharName and i want:
CharName CharStats = (CharStats)Player.tranform.Find("Mesh").GameObject.GetComponent("PlayerController") as CharStats;
The problem is I can't Declare CharName CharStats since CharName is a string. But CharName contains the name of a class (script). Let's say the script is named TestScript the line should become:
TestScript CharStats = (TestScript)Player.transform.Find ("Mesh").GameObject.GetComponent("PlayerController") as TestScript;
So my question how to convert the String CharName to be used as the class TestScript.
Thank You :)
I am currently using the switch case method but if I will continue the code will become VERY long so I'm sure there is a better way to do this without making a switch case with 100 cases.
Ok so I don't know what you are actually trying to accomplish - which I guess I need to because you can't do what you suggest there. There are a number of techniques that could be applied depending on the context and for sure you don't want to do it in a switch statement!
You have choices like:
Send$$anonymous$$essage
Interfaces
Inheritance
Let me understand what you are trying to achieve
Answer by Blink123123 · Mar 29, 2013 at 11:16 AM
Solved It ! To Call The Value Use This (In Case Someone Else Wants Help):
(float)CharStats.GetField ("VariableName").GetValue(typeof(float));