Hi,
I have a string array and I need to call functions using those strings + I need to give some parameter to it(like I have a method: SetSpawn(transform tr) and a string: “SetSpawn”.
What would be the best way to solve it?
Thank you for the answers!
I’m unclear what you’re trying to do.
Can you just use a for() loop to iterate all the strings and call whatever method you want?
Or do you want to just use Random.Range() to select one of the strings randomly, and use that one?
My problem is that I want to add parameters to them, and that is impossible by using Invoke.
I googled and found this:
Have you tried that approach?
I found it as well, but those solutions don’t solve my issue since I need to call functions from a string array.
ButtonActionClass.Invoke(FunctionNames[1],FunctionDelayTime);
Just to check: is there a reason the original array identifying the functions that you want to call is using strings instead of delegates?
But you cannot give parameters to a function using invoke
Thanks, I never heard about that ![]()
By the way, the only problem with it that I can only use that type of variable I set when declaring the delegates. I know, I can dodge it by a setting a string parameter so I can transform that almost to anything, but is there any better solution?
Just have your parameter be of type object. Or object[ ], if you need to support multiple parameters.
Action<object> example = (x)=>Debug.Log(x.ToString());
example(10f);
example("hello!");
Thank you