Calling function by string.

I know this question has been asked multiple times but I am unable to understand the logic behind reflections such as SendMessage(). So I will explain what I’m attempting to do and hopefully someone can shed light on this.

So, I have script1 and script 2. Script 2 has static functionA, static functionB and static functionC

I want script1 to call a function from script 2 based on an earlier calculation.
so effectively what I want is a function that reads a string as a function

eg.
{
string x = B;
call(“function.” + x);
}

You still haven’t really said what you actually need this for. You basically ask how to dynamically call a static method which is a contradiction in itself. Static methods are staticly linked. Of course you could use reflection as a last resort, but it should be avoided if possible.

C# or .NET in general is a compiled language / framework. With reflection you can have some dynamical elements, but it’s quite slow and almost impossible to debug / refactor.

Unity’s SendMessage only works with instance methods and not with static methods.

To use reflection you could do this:

using System.Reflection;

string x = "B";
System.Type script2Type = typeof(Script2);
MethodInfo methodInfo = script2Type.GetMethod("function" + x, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (methodInfo != null)
{
    methodInfo.Invoke(null, null);
}
else
{
    Debug.LogWarning("method with name: function"+x+" doesn't exist");
}

We can’t really suggest alternatives since we don’t know what’s your actual goal. You presented some very abstract example. Also when you want to improve your question, you should edit it and not post comments.