- Home /
turn string into function with arguments?
Is there a way besides eval to turn a string into a function with arguments, either in C# or javascript.
I want a string like this "somefunction(1,2)" to be somehow turned into the real function.
I use unityscript but if c# has a solution I could use that. I just don't know how to combine a c# function with a unityscript function.
Thanks,
Answer by bzgeb · Sep 21, 2012 at 08:44 PM
I believe SendMessage is more or less what you're looking for: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html
Otherwise maybe Invoke: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Invoke.html
If your string is in the format "somefunction(1,2)" then you'll need to parse the arguments in order to pass them to SendMessage. I'm not familiar with the string library in Unityscript, but you basically grab the substring between '(' and ')' then split on ',' and place it into a generic data structure so you can pass it as a parameter in SendMessage. Somebody may be able to help you with that.
Invoke doesn't allow you to pass arguments.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081323im_/https://answers.unity.com/themes/thub/images/avi.jpg)