C# Countdown timer outside of MonoBehavior

I have a POCO (Plain old C# Object) :slight_smile: that I use to manage a lot of math in my game. I am trying to figure out a way to add a count down timer to this without using Update(). I have searched the web for C# countdown timers and all of them rely on Timer() which is a windows implementation and I don’t even see it in the scripting reference. This timer doesn’t have to be atomic clock accurate… It’s maximum length is 3-4 minutes. So if it’s off by 4-5 seconds by the time 4 minutes rolls around, it’s okay. I’m trying to run this timer completely within a plain C# object. I’d like to avoid messages and calls from another MonoBehavior Update functions. Thoughts from smart people?

Might not be the best solution, but you could try making a function that just yields for a set amount of time, or for/while loops for the yielded time. It would freeze up that particular script from progressing, but would have a timer not depending on an update loop.
And if it’s a while or for loop, you could change that time it waits as it’s checking.

I use a couple of these kinds of “timers” in my code for simple things I don’t want to drag update loops into.

I got. it. You can indeed use Timer class in unity.

System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval=5000;
aTimer.Enabled=true;

// Then define OnTimeEvent to be called every 5000ms.

private static void OnTimedEvent(object source, ElapsedEventArgs e)
 {
     Debug.Log("Hello World!");
 }