Hello ! Strange thing happening with coroutine when I'm calling it for second time. It's like StartCoroutine continuing execution...not starting over. First I'm calling StartCoroutine, then StopCoroutine (in the mid of execution). After that i'm calling StartCoroutine for Second time - expecting that code should do "Debug.Log("Routine - Started");", but it DOESN'T, Execution continues from "Debug.Log("Routine - Time is up");" command. The result is on picture. Code is in spoiler. What's wrong with this code ? Spoiler: Script Code (CSharp): using System; using UnityEngine; using System.Collections; public class PGate : MonoBehaviour { private IEnumerator routine; private Action close; void Awake() { routine = WaitAndDo(10f, () => { }); } void ButtonDown(int internalID) { // Start Counting Debug.Log("Start"); StartCoroutine(routine); } void ButtonUp(int internalID) { // Stop Debug.Log("Stop"); StopCoroutine(routine); } IEnumerator WaitAndDo(float time, Action a) { Debug.Log("Routine - Started"); yield return new WaitForSeconds(time); Debug.Log("Routine - Time is up"); if (a == null) Debug.Log("Action is null."); else { Debug.Log("Call"); a(); } } }
Yeah, I think that's intended behavior, even though it doesn't seem to specifically say it in the docs. Stopping then restarting the same instance of an IEnumerator coroutine is like pausing then resuming it where it left off. If you want to start a new instance of the coroutine, you should move "routine = WaitAndDo(10f, ()=>{});" into ButtonDown.
thanks for making things clear ! I think your solution for me is the best and only Reset is the correct way, but it doesn't work (not implemented ) routine.Reset(); Thanks for replies !