- Home /
Problem is not reproducible or outdated
Coroutine skips WaitForSeconds
I am trying to implement an AI system in my game. It works by having 2 coroutines which run in loop constantly. First coroutine activates the second one. The coroutines are continuously being started/stopped.
Now, normally when second coroutine is started, there should be a delay until the rest of the method is executed (shown in second picture), and this works most of the time. However, occasionally, the yield return WaitForSeconds part gets skipped, and the coroutine code is executed immediatelly upon starting, leading to unpredictable behaviour.
Here is the part of debug.log: (execution is from bottom-up)
Here is the coroutine code:
Does anyone know if it is possible for Unity to skip wait for seconds, and what could be reason for that?
@Addyarb it is initialized in Awake () like this ActivationWait = new WaitForSeconds (Interval)
and Interval is public float variable I assign in inspector (0.25 here). But I think that even if Interval was 0 or if ActivationWait was null there would still be 1 frame of delay before executing the rest of the code so this confuses me.
Have you tried defining it within the coroutine? Or just using the more wasteful yield return new WaitForSeconds(Interval)'
to see if that works?
Answer by Topthink · Apr 04, 2018 at 02:00 PM
I wonder if "Invoke("Something",2)" -- or whatever fits your need -- would work there? Might be worth a try.
Answer by nemanjabr1 · Apr 04, 2018 at 02:05 PM
What is ActivationWait? Try yield return new WaitForSeconds(float); Did you call the method with StartCoroutine
Answer by cvetinovicnikola · Apr 04, 2018 at 03:12 PM
OK, I just did a bunch of testing and it seems that problem seems to be my CoroutineWrapper class. I made that class so I could easily track which coroutines are started and to prevent them starting multiple times or stopping an inactive coroutine. Here is how the class looks:
public class CoroutineWrapper {
public bool Running;
public Coroutine MyCor;
public IEnumerator MyIEnum;
public MonoBehaviour MyContext;
public CoroutineWrapper (MonoBehaviour context, IEnumerator myIEnum){
MyContext = context;
MyIEnum = myIEnum;
}
public void Start (){
if (Running) {
return;
}
MyCor = MyContext.StartCoroutine (MyIEnum);
Running = true;
}
public void Stop (){
if (!Running) {
return;
}
MyContext.StopCoroutine (MyCor);
Running = false;
}
I usually initialize this class on Awake () inside MonoBehaviour object like this CoroutineWrapper cor = new CoroutineWrapper (this, DoSomething())
and then call Start/Stop on it. However, if I stop it, and then call start again, yield return Wait... gets skipped for whatever reason. I guess passing IEnumerator as a parameter has something to do with it.
Anyways, I tried using StartCoroutine / StopCoroutine on normal Coroutine object and it works as expected, with correct waits. I'm still gonna have to test and see if I can make some changes to my code, but for now I will be exclude CoroutineWrapper and use normal Coroutine object.
This feels like information that should have been presented in the original post. :-|
Follow this Question
Related Questions
Coroutine doesn't work when called from another method. 3 Answers
Coroutine not running after yield return new WaitForSeconds 3 Answers
How can i get this Delay script to work? 1 Answer
Waypoint / Yield help 1 Answer
Coroutine isn't working? (C#) 2 Answers