How do you pass a void method into another method as a parameter?

This is part of a custom name space that will bring Invoke methods to editor scripts. I already have “StartEditorCoroutine” working. The problem is that I can’t pass void in as a parameter as seen below.
I have tried changing void to Event but no luck.

thanks in advance

public static EditorTimers EditorInvoke(void method, float time){

            }

Example

I think you’re looking for the Action type…

public static EditorTimers EditorInvoke (Action method, float time) {
    // ... bla bla...
    method.Invoke()
    //... bla bla...
}

Then if you need to pass in parameters…

public static EditorTimers EditorInvoke (Action<float, int, MyObject> method, float time) { }

What Namespace has the action class? MonoDevelop doesn’t seem to recognize it.

System

I’m Using:

            public static void EditorInvoke(Action method, float time){
                method.Invoke();
            }

And I get this error.

error CS1503: Argument #1' cannot convert void’ expression to type `System.Action’

You’re probably calling it like EditorInvoke (RandomMethod(), 1.0f); try calling it like EditorInvoke(RandomMethod, 1.0f); without the () after the method. Adding () to a method calls the method instead of referencing the method itself.

1 Like

You could also be using a delegate, which is a lot more straightforward in terms of syntax (IMO) and you don’t need to pull in extra namespaces.

//first, lay out the 'template' of the function:
public delegate void SomeDelegate(int whateverParameters);
//then, the function itself, which is declared as normal
public void SomeFunction(int theSameParameters) {
//do stuff
}
//and now, passing and using it
public void TheMainFunction(SomeDelegate theDelegate) {
theDelegate(3); //call it just like 'theDelegate' was the function name
}

void Update() {
TheMainFunction(SomeFunction);
}
3 Likes

You made me realise the brackets after days of frustration, thank you!