Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Last active April 6, 2025 20:50
Show Gist options
  • Save kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e to your computer and use it in GitHub Desktop.
Save kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e to your computer and use it in GitHub Desktop.
Call After Delay - handy Unity3D coroutine to do something later
using UnityEngine;
using System.Collections;
public class CallAfterDelay : MonoBehaviour
{
float delay;
System.Action action;
// Will never call this frame, always the next frame at the earliest
public static CallAfterDelay Create( float delay, System.Action action)
{
CallAfterDelay cad = new GameObject("CallAfterDelay").AddComponent<CallAfterDelay>();
cad.delay = delay;
cad.action = action;
return cad;
}
float age;
void Update()
{
if (age > delay)
{
action();
Destroy ( gameObject);
}
}
void LateUpdate()
{
age += Time.deltaTime;
}
}
@ATGeorge
Copy link

ATGeorge commented Apr 5, 2025

I have seen this person comment on coroutine functionality and use cases on several different threads, and this is one of many examples of alternatives to coroutines that they have made available. They preach caution around using coroutines and I have gone back and forth on their philosophy for years now. I'm sure I can't put it as eloquently (or informatively) as they can, but my understanding is that coroutines, while convenient, can quickly become unmanageable if relied on too heavily and they can introduce bugs if used in situations more complex than the simplest "set it and forget it" scenarios.

If your project is something extremely small or is a prototype, then I'd go with coroutines to get it working, but if you want scalability and precise execution control, you should probably take the time to implement something like this in most cases.

A whole coroutine brain dump of theirs that I just came from:
https://discussions.unity.com/t/coroutine-for-heavy-operation/886052/2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment