C# Concatenate to call method?

I’m trying to concatenate two strings to call a method.

string replay;

string letter = "A";
string number = "1";
string col = "();";


replay = string.Concat(letter,number,col);

replay; //would this call a the A1() method?

No, it won’t do anything (if you’re lucky, you’ll get the compiler yelling at you with error messages though). At best, its just a string and nothing else.

If you want to call a method based on a string, check out Invoke or SendMessage instead.

There’s also a video on Invoke, here: http://unity3d.com/learn/tutorials/modules/beginner/scripting/invoke

It is possible to generate code on the fly which can be called with Reflection Emit. You can also use Reflection to obtain a function based off a string which you can later call.

No, it won’t. It won’t even compile.

If you need to call methods using concatenated strings, you have to look at MonoBehaviour.Invoke method or eventually learn about .NET reflection. But depending on your goal, these solutions might not be perfect (performance). If you can share what is your goal, maybe I can suggest alternative solution.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour 
{
	string letter = "A";
	string num = "1";
	string method;
	
	void Start()
	{
		method = letter+num;
		Invoke (method, 0);
	}


	public void A1()
	{
		print ("Succeded.");
	}
}