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?
Answer by LukaKotar · Jan 06, 2013 at 05:04 PM
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 I do this solution unity crashes any thoughts?
@jointothedarkside Your loop is most likely trying to run infinite times in a single frame, making your game freeze. You can do yield return null
, which will make the loop wait until the next frame before continuing.
while(true){
// Do stuff here
yield return null; // Wait for the next frame before looping over
}
actually i saw this other solution where they recommended to use while(true){ // Do stuff here yield return null; // Wait for the next frame before looping over } yield return null;
And If you do so you get a warning for the second return null, I'm kind of lost here
If you never stop the loop (either with the break
keyword or by having the condition turn false), then all code after the loop is unreachable. I'm guessing that's why you're seeing the warning. You don't need the second yield return null
.
it's not working for me. it only works when i press a button to go to another scene :(
Answer by AlexHogan · May 01, 2015 at 04:07 AM
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.
Answer by Owen-Reynolds · Jan 06, 2013 at 08:44 PM
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.
well this makes my functions do nothing, creative approach or maybe im doing something wrong
Answer by Eric5h5 · Jan 06, 2013 at 05:06 PM
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
}
Answer by bullze · Apr 11, 2017 at 08:41 AM
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
}
This is not recommended as it will generate a lot of garbage since you constantly starting a new coroutine. If you want an endless running coroutine, just use a while loop like Luka$$anonymous$$otar showed in his answer.