how to make System.Action invoking?

it is in a static class so I cannot use mono invoke.

using System;
// ...
public static void Invoke (){
	Action t = Test;
	t.Invoke();
}
private static void Test(){
	Debug.Log("WORKS");
}
}

this seems to invoke the function immediately.

how would I make it invoke repeating OR how would I make it invoke after a certain time?

thanks in advance.

It is possible to introduce a delay using threads (as Benproductions1 stated), but the real question is what do you want to do inside the method you want to invoke. It will be executed on another thread, so no Unity specific stuff will be allowed (no GUI, no Instantiate, etc.)

For example, the following code works:

public static void Invoke(int delay)
{
    Debug.Log("Started...");
    Action t = Test;
    Action delayAction = () => Thread.Sleep(delay);
    delayAction.BeginInvoke(iar => t(), null);
}
private static void Test()
{
    Debug.Log("WORKS");
}

But if you add UnityEngine.GameObject.CreatePrimitive(PrimitiveType.Cube) to the Test method, it will fail. That’s because Test method will not be running on main thread. So if you want something like this, you have to look for another solution.

Additionally, this won’t work in all cases - for example, in Win8 application, you’d have to change this completely, because Thread.Sleep is not supported.

EDIT: of course you can skip Action t declaration and just call Test() instead of t()

If your class is still inside a Unity project you can use a coroutine like this:

IEnumerator _InvokeDelegate(System.Action aAction, float aDelay)
{
    yield return new WaitForSeconds(aDelay)
    aAction();
}

public void InvokeDelegate(System.Action aAction, float aDelay)
{
    StartCoroutine(_InvokeDelegate(aAction, aDelay));
}

And finally invoke it like this:

    Action t = Test;
    InvokeDelegate(t,3); // invoke in 3 seconds

If another non MonoBehaviour class should be able to use this, make the MonoBehaviour which contains the coroutine a singleton.

edit I finally put my CoroutineHelper package on the wiki (the polishing took hours :D). It provides several quite useful methods to run a delegate different ways.

Here’s the example scene that’s included in the package. This whole example just uses the Start method of one script and everything else is created within the method itself using closures and my coroutine helper pack.