So basicaly I was wondering if there’s a way to write a function suspending the execution of another funtion, for example

void Foo()
{
// do something
}

Suspend(Foo,5); //Foo() will be executed after 5 seconds

So something like the following…except it doesn’t work :slight_smile:

public class SuspendScript<T> : Monobehaviour
{
 public delegate void MyDelegate(T parameter);
 public MyDelegate functionToSuspend;
 public T parameter; // the parameter of the suspended function

public void Suspend(float time)
{
 StartCoroutine(SuspensionCoroutine(time));
} 

IEnumerable SuspensionCoroutine(float time)
{
yield return new WaitForSeconds(time);
functionToSuspend(parameter);// execute suspended function after *time* seconds
}
}

and now the calling function

      public class SomeClass : MonoBehaviour
        {
        SuspendScript<string> SS;
        void JustForGigs(string s)
        {
        Debug.Log(s);
        }
        
        void Start()
        {
        SS=new SuspendScript<string>();
        SS.functionToSuspend=JustForGigs;
        SS.parameter="Haha!";
    SS.Suspend(5); //JustForGigs will be executed after 5 seconds
        }

Expected outcome:
Debug.Log(“Haha!”)

Actual Outcome:
NullReferenceException :frowning:

yeah so my question is if it’s even possible to suspend a function like this? And if it is indeed possible then what am I doing wrong? Is there a better way?

Another way of doing it could be using a simple function in the same class, just depends on your needs:

using UnityEngine;
using UnityEditor.VersionControl;
using System.Collections;
using System;

public class Coroutines : MonoBehaviour {

	void Start () {
        StartCoroutine(Suspend((s) => JustForGigs("Haha!"), 5f));
	}

    public void JustForGigs(string s)
    {
        Debug.Log(s);
    }
	
    public IEnumerator Suspend(Action<Message> d, float time)
    {
        yield return new WaitForSeconds(time);
        d.Invoke(new Message());
    }
}

The Action delegate is in the form of (random parameter variables) => FunctionName(actual parameters).

Yeah so FortisVentalier gave me the answer I needed and I managed to create a working suspension script, check it out

public class SuspensionScript<T> 
{

	public delegate void MyDelegate(T value);
	public MyDelegate function; //the function to suspend
	public T param; // the parameter of the function to suspend


	public IEnumerator SuspensionCoroutine(float time)
	{
		Debug.Log ("MyCoroutine");
		yield return new WaitForSeconds (time);
		function (param);
		yield return null;
	}

}

caller function: (you may use any datatype instead of string, just put it in the <> )

    public class SomeClass : MonoBehavior
    {
    
    SuspensionScript<string> SS;
    
    void JustForGigs(string s)
    {
    Debug.Log(s);
    }
    
    void Start()
    {
    SS = new SuspensionScript<string>();
    SS.function = JustForGigs;
    SS.param="Haha!";
StartCoroutine(SS.SuspensionCoroutine(5)); //JustForGigs will be executed after 5 seconds
    }
    
    }

Have fun making Time Bombs and stuff