SoFunction
Updated on 2025-03-07

Summary of various methods for delayed execution of Unity

When I first came into contact with unity, I used the timer to do delays. Later I discovered that there are so many more convenient ways to delay execution for our carbide. Now let’s take a look.

There are the following four types listed in this article. If there are any errors and omissions, please feel free to add corrections.

Update Invoke Coroutine DoTween
Support parameter incoming ✖️ ✖️ ✔️ ✖️

The following table shows the execution status of each plan under different circumstances

Update Invoke Coroutine DoTween
= 0 ✔️ ✖️ ✖️ ✔️
activeSelf == false ✖️ ✔️ ✖️ ✔️
enabled == false ✖️ ✔️ ✔️ ✔️
destroy ✖️ ✖️ ✖️ ✔️

Update timer

Timer in refresh functions such as Update, timer += executes the method that requires delay when the time reaches the expected time. It should be noted that the increment time is a floating point number that is different for each frame. The timer cannot be used == to determine the conditions achieved. You need to add a flag with > or >=. To ensure that the function can be executed and only once, Update will be executed normally when = 0, but the value at this time is 0. If you need to use the timer at this time, you can use it

 private float delayTime = 1;
    private float timer = 0;
    private bool executed = false;
    void Update(){
        if (executed == false){
            timer += ;
            if (timer >= 1){
                executed = true;
                DelayFunc();
            }
        }
    }
    private void DelayFunc(){
        print("Execute test function");
    }

Invoke

The Invoke() method is a delegate mechanism of Unity3D. Easy to use. But what's different from other methods is that hiding objects or setting script enabled to false will not interrupt Invoke's execution. Invoke supports repeated calls: InvokeRepeating ("Method Name", delay time, interval between calls)

 private float delayTime = 5;
    private void Start(){ 
        Invoke("DelayFunc",delayTime);
        //InvokeRepeating("DelayFunc",delayTime,cdTime);
    }
    private void DelayFunc(){
        print("Execute test function");
    }

Coroutine

Coroutines are essentially iterators, which are troublesome to write, but they are very useful. It can delay fixed time or delay fixed frames.
Click here to be a coroutine management class, which supports functions such as pause and continue.

  private float timer = 0;
    private void Start()
    {
        StartCoroutine("DelayFunc");
         = 0;
    }
    IEnumerator DelayFunc()
    {
        while ( timer < 30)
        {
            yield return new WaitForSeconds(1);
            timer++;
            print("Execute test function"+timer); 
        }
    }

DoTween

If you use UGUI, then you must know DoTween. DoTween is a very common simple animation plugin, but sometimes we can use it to do some weird operations. For example, after the delay function is executed, it can only be closed through the built-in functions such as (); (); (;). = 0, if you hide objects, disable scripts, delete objects, etc., continue to execute the method.

 private void Start()
    {
        seq = ();
        (5);
        (DelayFunc);
        (false);
        (true);// When set to true, it can be executed normally under =0        
    }
    void DelayFunc()
    {
        print("Execute test function"); 
    }

This is the end of this article about the various methods of Unity delay execution. For more related Unity delay execution content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!