In this example, I use the Timer to delay the GameOver state by 2 seconds. This makes it so in my game, when the final player dies, there is a 2 second delay before the game switches to the GameOver. This small delay has benefitted gameplay by allowing the players to process how they died, and was easily and cleanly implemented with the above pattern.

Garbage

There's one annoying problem with this pattern of implementation.

It allocates 408 bytes of garbage every time Initialize is called. It seems to be related to passing a delegate as a parameter.



To get around this, the caller should cache the delegates in it's Start or Awake methods. This way, no garbage is allocated at runtime. It pains me that the caller has to do this, because the Timer is supposed to eliminate the kind of setup that a basic timer requires, but this still cleans up your Update function greatly.

Another garbage free implementation would be to have the callbacks be specified by an interface, and have the caller implement TimerUpdate and TimerComplete methods, then the caller could simply supply a "this" reference to the Timer's Initialize method. There's pros and cons to both implementations.

A garbage free usage of the Timer looks like this: