Execute coroutine in Update()

Hi!

I’ve just get into Unity and trying to write first game concept.

In the concept there are some random events generated in determined time (for example 8-12 seconds). So i try this:

    void Update()
    {
        StartCoroutine(GenerateEvent());
    }

    IEnumerator GenerateEvent()
    {

        // wait for 8-12 seconds
        // select new point
        // show a popup badge 
        // work with popup

        int _wait = Random.Range(8, 12);
        yield return new WaitForSeconds(_wait);

        // Below are bunch of lines i hope doesn't matter about the question

    }

But it doesn’t work as i expect. I need to do something every X-Y seconds.

I tried to write something like:

    IEnumerator DebugEnumerator(float delay)
    {
        yield return new WaitForSeconds(delay);
        Debug.Log("Waited for: 1 second (DEBUG)");
    }

But it behaves in Update() like waits for one second and then executes every frame, not every second.

Probably i misunderstood the concept of coroutine. Can you tell me the way?

Update() cannot be a coroutine, but you could create a while loop inside of an IEnumerator to achieve your desired behavior. Start the coroutine in Start(), and the loop will cause it to repeat once it reaches the end.

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

IEnumerator Example () {
	while(true){ // This creates a never-ending loop
		yield return new WaitForSeconds(2);
		// Do stuff here
		// If you want to stop the loop, use: break;
	}
}

If you’re just starting Unity, maybe skip coroutines completely for now. The best way to do something is a way that makes sense to you. See if this seems more obvious, counting time down to 0, then resetting:

public float secsToNext=0.0f; // public lets you check it running in the Inspector

Update() {
  secsToNext -= Time.deltaTime;  // T.dt is secs since last update
  if(secsToNext<=0) {
    secsToNext = Random.Range(8.0f, 12.0f);
    // do thing here:
  }
  ....

A coroutine automatically does the countdown for you, so runs a little faster. But if things are allready fast enough, there’s no point complicating things.

Your Coroutine is fine - nice jobe. The problem is that update is sending the event every frame, so your coroutine is starting every frame.

Just change

void Update()
     {
         StartCoroutine(GenerateEvent());
     }

to be

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

So then your event will only get fired once.

You should not use Update for this, since Update always runs once every frame and cannot be delayed in any way.

IEnumerator Start () {
    yield return new WaitForSeconds (Random.Range (8, 12));
    // Do stuff
}

You can do it like

bool isCoroutineReady = true;
void Update()
{

  if(isCoroutineReady)
  {
     isCoroutineReady = false;
     StartCoroutine(yourCoroutine());
  }

}

IEnumerator yourCoroutine()
{

    //your Code

    isCoroutineReady = true;
    yield return null
}

Why use InvokeRepeating Unity - Scripting API: MonoBehaviour.InvokeRepeating

From the documentation: Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.

Although it is very old question, I think I found a way that actually works:

void Update()
{
     StartCoroutine(GenerateEvent());
}

IEnumerator GenerateEvent()
{
     enabled = false;
 
     int _wait = Random.Range(8, 12);
     yield return new WaitForSeconds(_wait);
 
     enabled = true;
}

enabled only enables/disables Update() and it does not affect the CoRoutine so by putting enabled = false and enabled = true in, it would not run every frame. (However, thing I do not know is if it would affect the performance too much, so it may not be the best answer.)

Hello I didn’t really read a question but if you want start coroutine in update wait until it finish and start it again then maybe like this:
if (waitAndSpin == null)
{
waitAndSpin = WaitAndSpin(2);
StartCoroutine(waitAndSpin);
}
It will automatically destroys after coroutine is finished and start again.
Hopefully it will help someone :),I didn’t exactly read a question but if you want start coroutine in update and not start in every frame then maybe like this.

if (waitAndSpin == null)
        {
            waitAndSpin = WaitAndSpin(2);
            StartCoroutine(waitAndSpin);
        }`

It will automatically destroys after it’s finished and start again wait until it’s finished and starts again and so.