-
-
Save kwalkerxxi/646efa3dc301b60ade1e83211c4dd9a3 to your computer and use it in GitHub Desktop.
Call After Delay - handy Unity3D coroutine to do something later
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment