IEnumerator working only once

Hello, I have a problem where I have an IEnumerator that’s called only once and then don’t work

public IEnumerator WrongLocking()
    {
        Frame[currLock].color = new Color32(255,0,0,255);
        yield return new WaitForSeconds(0.1f);
        Frame[currLock].CrossFadeColor(Color.black, 0.3f, false, true);
    }

I tried to put it in the actual button method but it didn’t work either.

public void LockPosition()
    {
        if (Slider[currLock].value >= minGreen && Slider[currLock].value <= maxGreen)
        {
            LockButton[currLock].interactable = false;
            Handle[currLock].color = new Color32(84, 255, 84, 255);
            StartLockpick();
        }
        else
        {
            isRuined = true;
            StartCoroutine(WrongLocking());
            IEnumerator WrongLocking()
            {
                Frame[currLock].color = new Color32(255, 0, 0, 255);
                yield return new WaitForSeconds(0.1f);
                Frame[currLock].CrossFadeColor(Color.black, 0.3f, false, true);
                coroutineSwitch = false;
            }
        }
    }

Is there a way how to call this method more than once or is there any other option how to do that?

You have two WrongLocking() coroutines, one of them inside the else block of your LockPosition() function.

Remove the one within the other function. You don’t have a need to nest functions here.

One easy way to see if something is running is to put lots of Debug.Log() statements in your code.

Some ways:

  • Call it multiple times in a loop
  • have a timer (float) that decides when to call it again (in Update())
  • use InvokeRepeating() ( personally I don’t like this one because it is opaque to understanding / debugging)
  • etc.