How can i pass a method like a parameter Unity3d C#?

i’m trying to pass a function to another function as a parameter, how make this work?

This is an example of what i need to do:

CreateButton( new BuildModeController().BuildObject_BuildInstalledObject("Wall") );

How i must write the method?

void CreateButton( [What i need to write here?] )
     . . . do something.

Have in consideration than BuildObject_BuildInstalledObject("Wall") doesn’t return anything (yes, is a void method), an itself requeire a parameter, this method actualy make something in the worldmap

this is the actual CreateButton method:

void CreateButton(Canvas panel, Vector2 newVector2Position, Vector2 newVector2size, Action<string> method, Sprite buttonSprite, string stringNameButton)
    		DicButtonSprite.TryGetValue ("TileBlancoTibia", out ButtonSprite);
    		//Create button & Text
    		Button button = (new GameObject ("Button", typeof(CanvasRenderer), typeof(Image), typeof(Button))).GetComponent<Button> ();
    		Text txt = (new GameObject ("Texto", typeof(CanvasRenderer), typeof(Text))).GetComponent<Text> ();
    		//Installing Hierarchys
    		button.transform.parent = panel.transform;
    		txt.transform.parent = button.transform;
    		//Posición y tamaño
    		button.transform.position = newVector2Position;
    		button.GetComponent<RectTransform> ().sizeDelta = newVector2size;
    		txt.transform.position = newVector2Position;
    		txt.GetComponent<RectTransform> ().sizeDelta = newVector2size;
    		//txt preparations
    		txt.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
    		txt.alignment = TextAnchor.MiddleCenter;
    		txt.text = stringNameButton;
    		txt.color =;
    		txt.fontSize = 12;
    		button.GetComponent<Image>().sprite = buttonSprite;
    		//Metodo al que llama
    		button.GetComponent<Text>().text = stringNameButton;

of course, this method doesn’t work, and i’m looking to change the Action<string> method part

i’m trying to archive this to add to a button, through an addListener, if you have that in consideration would be excelent.

Thanks in advance

Here’s an example of how you would pass a method along as a parameter.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour 
    public delegate void TestDelegate(); // This defines what type of method you're going to call.
    public TestDelegate m_methodToCall; // This is the variable holding the method you're going to call.

    void Start() 
        // Fill Delegate.
        m_methodToCall = AwesomeExampleMethod; // Notice we don't use (); here. (that can be confusing)

        // Call method, pass along delegate.
        SimpleMethod( m_methodToCall );
    // This method expects a TestDelegate variable, allowing us to pass a custom void method.
    private void SimpleMethod(TestDelegate method)
        Debug.Log( "I'm about to call a method" );

    // A random method that is compatible with TestDelegate (asks no parameters, returns void)
    private void AwesomeExampleMethod() 
        Debug.Log( "Yay!" );

I hope that helps, if it did, please accept my answer.
If you need any details, let me know!

Best of luck!



for me the ans was

public Button button;

public void Func(UnityEngine.Events.UnityAction Func2){

     // i wanted to use it to change button on click listener



You can use delegates in c# to pass functions as an argument to other functions.

public delegate int PerformCalculation(int x, int y);
void CreateButton(int x)