How do you store a function in a variable in c#?

I want to be able to store a function in a variable, so that later i can call it from that variable?

Use and Action if the function does not return a value and a Func if it does
https://stackoverflow.com/questions/7367152/dynamically-assign-method-method-as-variable

**

private Func<int, int, out int> myFunction;

int Add(int a, int b){
    return a + b;
}

int Subtract(int a, int b){
    return a - b;
}

void DoStuff(){
    int a = 10;
    int b = 5;

    myFunction = Add;
    Debug.Log(myFunction());

    myFunction = Subtract;
    Debug.Log(myFunction());    
}

Hey,

this question has already been answered (check the link below), let me know if the article helped you or if you need further help :slight_smile:

https://answers.unity.com/questions/925943/storing-a-method-in-a-variable.html