Leave/End the IEnumerator the right way.

Hi there,

i have an IEnumerator which has to do a ColorCheck.

IEnumerator StartColorCheck()
{
	if(checkingColors)
	{
		//Debug.Log ("can't StartColorCkeck");
		yield break;
	}
	checkingColors = true;

	yield return new WaitForSeconds (0.5f);

	for (int i = 0; i < allcards.Length; i++)
	{
		CC = allcards*.GetComponent<ColorCheck>();*
  •   	CC.CheckColors();*
    
  •   	CardMatches += CC.matches;*
    
  •   }*
    
  •   checkingColors = false;*
    
  • }*
    The Problem is, it seems to stack up, until all calls are gone through it. I call it from a
    void OnMouseUp()
    but when i click to fast for example, all clicks seem to stack until all ran through it. Is there a way to avoid that? Because CardMatches returns a wrong value while it’s repeating all over and over.
    Thanks in advance.

I don’t really understand your problem. Doesn’t the above example work?

Note you could also replace that with a normal function and use Invoke.

void OnMouse()
{
     SingleInvoke("ColorCheck", 0.5f); // Call ColorCheck after half a second.
}

void SingleInvoke(string method, float time)
{
    CancelInvoke(method); // If method is pending, cancel it.
    Invoke(method, time);
}

void ColorCheck()
{
     for (int i = 0; i < allcards.Length; i++)
     {
         CC = allcards*.GetComponent<ColorCheck>();*

CC.CheckColors();
CardMatches += CC.matches;
}
}
[CancelInvoke][1], [Invoke][2]
[1]: Unity - Scripting API: MonoBehaviour.CancelInvoke
[2]: Unity - Scripting API: MonoBehaviour.Invoke