I am trying to insert delay using WaitForSeconds function as follows:
using System;
using System.Collections;
using UnityEngine;
public class MyClass: MonoBehaviour
{
void Update ()
{
// line 1
// line 2
// line 3
StartCoroutine("InsertDelay");
// line 4
// line 5
}
IEnumerable InsertDelay()
{
yield return new WaitForSeconds(5);
}
}
I read a number of threads but could not implements this.
Ideally, the control should go to line 4 after waiting for 5 seconds but it jumps immediately to it without waiting.
Is there anything wrong in the code?
The coroutine will halt for five seconds before continuing, not the actual Update. Think of coroutines as spawning a thread. The code you posted is quite hazardous as it will spawn a new coroutine each update. Use a timer (in Update) instead of a coroutine if you want to halt the Update execution.
Another way would be to just move all the update code into the coroutine. That way you get the behaviour you wanted in the first place.
Also, unless you intend to call StopCoroutine( “yourFunction” ), try not to invoke coroutines with their string name. It’s slower and produces memory overhead.
using System;
using System.Collections;
using UnityEngine;
public class MyClass: MonoBehaviour
{
void Update ()
{
StartCoroutine( InsertDelay() );
}
IEnumerable InsertDelay()
{
// line 1
// line 2
// line 3
yield return new WaitForSeconds(5);
// line 4
// line 5
}
}
So? It’s not any more expensive then running other code in the Update function, and, as I said, when not initiated by a string it does not produce as much overhead. As long as the profiler does not say that this is a problem for your game, I’d say don’t worry about it.
using System;
using System.Collections;
using UnityEngine;
public class MyClass: MonoBehaviour
{
IEnumerator Start()
{
// wait one frame ( where update would be called )
yield return new WaitForEndOfFrame();
// now do the update
do {
if( enabled ){
// line 1
// line 2
// line 3
yield return new WaitForSeconds(5);
// line 4
// line 5
}
yield return new WaitForEndOfFrame();// go to next update frame.
}while(true);// will break out with destruction.
}
}
That would be like what your trying to implement.
When you change Start to a IEnumerator return type, it starts it like a Coroutine. You wait the first frame to get where update would have been called. And you check for enabled if you want to tie it in like how Update would be called as well as waiting one frame between.
Problem with this however is enabled checkbox probably wont show up in unity without Update, FixedUpdate, OnGUI or other messages. If its important for it to show up, you could put in “void Update() {}”.
Yea, you could do that as well. I don’t actually know which is better performance wise. but using IEnumerator on start is definitely cleaner and easier
Because it wouldn’t actually do what he’s asking for… he actually wants to pause execution of the Update() function (and therefore, probably wants to use the CoUpdate method Eiznek posted).