How to Use System.Timers.Timer With unity GameObjects

Find out how Timer works… does it call that delegate in another thread?

If so, that will NEVER work with Unity. With Unity 100% of EVERYTHING you do must be on the main thread.

Use coroutines.

Coroutines in a nutshell:

Splitting up larger tasks in coroutines:

Coroutines are NOT always an appropriate solution: know when to use them!

Our very own Bunny83 has also provided a Coroutine Crash Course:

https://answers.unity.com/questions/1749615/coroutines-ienumerator-not-working-as-expected.html?childToView=1749714#answer-1749714

2 Likes