C# Call a Function by a String with Parameters / Invoke a Function with parameters?

Hi,

I try to create a Button in WorldSpace. The Button is triggered by a Raycast when the Player press the LMB.
Then a function is called, that call the Action Function, which I have defined in public variables.

The Variables :

	public MonoBehaviour ButtonActionClass; 

	public string FunctionName;

	public float FunctionDelayTime = 0.0f;

The Function:

public void TriggerButton ()
{
	if(!hasCondition)
	{
		ButtonActionClass.Invoke(FunctionName,FunctionDelayTime);
	}
}

This code works pretty well, but the problem is that I can’t add parameters with this Invoke function.

So I tried something different :

public void TriggerButton ()
{
	if(!hasCondition)
	{
		// ObjArray is a private Object[] ObjArray; in which I've added a few Variables

		ButtonActionClass.GetType().GetMethod(FunctionName).Invoke(FunctionName,ObjArray);
	}
}

But the Problem is that I have to replace every Target Function parameters to an Object and convert them, if this is possible at all.

So I hope that a solution can be found.

PS : Sorry for my English

Since you can not call Invoke with parameters you can use StartCoroutine for this.

Say your code is:

 void Start()
 {
     Invoke("MyFunction", 1f);
 }
 
 void MyFunction()
 {
     // Do your thing.
 }

Instead you can use Coroutine by passing argument like:

 void Start()
 {
     StartCoroutine(MyFunction(false, 1f));
 }
 
 IEnumerator MyFunction(bool status, float delayTime)
 {
     yield return new WaitForSeconds(delayTime);
     // Now do your thing here
 }

Today I finally could test the code, but I realised that I can’t use a variable with StartCouroutine() and I get the following error : … is a field but a `method group’ was expected

ButtonActionClass.StartCoroutine(FunctionName( TransformInput, GameobjectInput...));