Coroutine not working on second call
I have a function with a StartCoroutine, however when the function is called again the StartCoroutine does nothing. I'll leave the code here:
public Animator animController;
IEnumerator AfterFlip, FlipBack;
// Use this for initialization
void Start () {
AfterFlip = afterFlip();
FlipBack = flipBack();
}
// Update is called once per frame
void Update () {
}
public void flipItem()
{
if(animController.GetBool("Wait")==true && PieceController.A1 == false) {
StartCoroutine(AfterFlip);
Debug.Log("HelloThere");
}
else
{
StartCoroutine(FlipBack);
}
}
IEnumerator afterFlip()
{
animController.SetBool("Flip", true);
PieceController.A1 = true;
yield return new WaitForSeconds(0.2f);
animController.SetBool("Flip", false);
}
IEnumerator flipBack()
{
animController.SetBool("Wait", false);
PieceController.A1 = false;
yield return new WaitForSeconds(0.2f);
animController.SetBool("Wait", true);
}
I am using a bool from another script, I tried seeing if that was the reason but the Debug.Log kept sending the message.
Answer by Bunny83 · May 03, 2018 at 10:59 PM
A coroutine is not a method but it's a statemachine object. You create one statemachine in start. Once the IEnumerator object has actually reached it's end, passing the same statemachine again to Unity of course does nothing since the statemachine has already reached the end, You have to create a "new" statemachine:
StartCoroutine(afterFlip());
If you want to better understand coroutines (or C# iterator blocks in general) you may want to have a log at this archived blog post. Unfortunately the original blog doesn't exist anymore.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161912im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Coroutine Issue 1 Answer
c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 2 Answers
Coroutine working in Play Mode, but not on build 0 Answers
IEnumerator inner functions 0 Answers
Calling coroutine in a method issue. 1 Answer