- Home /
Coroutine confusion
Although this may have already been answered elsewhere, I am still struggling to create delay's in code execution and apparently Coroutines are the best way to do this. In my example: Previously written main code, At a point where I decide I need to pause until continuing , Proceeding code.
I have tried using coroutines for the delay but I don't want to have to put all of the code following the delay into the coroutine, just pause the execution of my main code before continuing with it.
I do understand that this is not the clearest explanation but any help is appreciated.
For simple delays, a simple Invoke can be enough.
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html
Answer by DiegoSLTS · May 30, 2016 at 01:32 PM
You can't pause the execution of a normal function unless you use threads, but threads are not recomended in Unity since doing things outside the main thread is complicated.
If you don't want to move all the code into the coroutine you can do this. Let's say this is your code:
void SomeFunction() {
//some code
//you want a delay here
//some more code
}
Turn that into:
void SomeFunction() {
//some code
StartCoroutine(Delay(2f,SomeMoreCode)); //delay of 2 seconds, pass the value you want
}
void SomeMoreCode() {
//some more code
}
IEnumerator Delay(float seconds,UnityAction callback) {
yield return new WaitForSeconds(seconds);
callback();
}
This way you can delay any function for any amount of seconds from any other function in your code.
This way you can delay any function
Not really with the code you have given. Only functions that do not require arguments. The Invoke way would be easier here :
void SomeFunction() {
//some code
Invoke("Some$$anonymous$$oreCode", 2f) ;
}
void Some$$anonymous$$oreCode() {
//some more code
}
I always prefer to do things that don't require using the names of methods as strings, it's easier (not much here, just a 2 line function) but more prone to errores in the future.
Anyway, I wouldn't do a coroutine that only delays and don't use Invoke for the string parameter, I'd just make "SomeFunction" a coroutine, do the first part, yield return for some seconds and do the second part.
You're right about the "any function", the callback can't receive parameters.
Yes, I also avoid using string to call a function. It's more difficult to know where my function is called, and who calls it.
Ins$$anonymous$$d of using a Unity action, a custom delegate could be a good choice.
Is there no way to say, Invoke to a function after 'x' time that simply logs something to the console and then bounce back to where Invoke was called and continue?
Unfortunately, this behaviour is not possible using Invoke. Only coroutines are possible here. See DiegoSLTS's answer ins$$anonymous$$d.
Apparently, UnityAction as a namespace could not be found.
Yes, sorry. UnityAction is in the UnityEngine.Events namespace. Either add that as a using at the top or write UnityEngine.Events.UnityAction ins$$anonymous$$d of UnityAction as the coroutine parameter type.