Best way to wait for time on loop?

I tried the manuals WaitForSeconds example but it didn’t seem to work for me.

IEnumerator startTime()
    {
        Debug.Log("Starting time");
        yield return new WaitForSeconds(3);
}

I placed this into both my Start() function and the update function (separately of course) and it never logged it out. The manuals example puts it into another IEnumerator thing. Perhaps part of it is that I’ve never used an IEnumerator “thing” before. Not sure the purpose or why it is used. But what I need is for it to constantly loop and have some code run after it is finished. I know how to do the looping part, and the changes I need for later, but I can’t get the WaitForSeconds to work right. Wish there was an easier way to do this, database languages imo have waiting down. Thanks!

This “IEnumerator thingy” is called a coroutine in Unity. You use it like this:

void Start() {
  StartCoroutine(startTime());
}

IEnumerator startTime() {
  Debug.Log("Start wait");
  yield return new WaitForSeconds(3f);
  Debug.Log("End wait");
  // Do something else
}

Google “Unity Coroutines” for more information. It’s basically Unity exploiting a language feature of C# called iterator functions to pause and resume execution of a function.

Thank you again Praetor definitely the savior of the Unity forums. Definitely cleared that up.

Sure thing. Happy coding. Feel free to post back if you have any issues with it.