G
G
Gagatyn2020-05-03 21:35:31
Unity
Gagatyn, 2020-05-03 21:35:31

Why does the code keep running after StopCoroutine()?

Hello. Flashing implemented through 2 buttons: 1 dark button, 2 light button.
The first is SetActive(false), the second is SetActive(true), and so it alternates...

After that StopCoroutine(), the blinking stops and continues again.
If I pass the name of the function as a string StopCoroutine("myFunc"), then everything works, but thanks to this I will have to duplicate the functions: "myFunc2", "myFunc3"... because there are more than 2 buttons, I don't want to duplicate them.

How to stop flashing?
How to write a generic function for coroutines?

// class Btns
static public Btns S;
private IEnumerator _coroutine;

public void startFI () {
  _coroutine = BlinkindRun (btn1, btn2);
// 1. пробовал _coroutine = StartCoroutine ( BlinkindRun (btn1, btn2) );
  StartCoroutine (_coroutine);
}

public void stopFI () {
 // 2. пробовал _coroutine = BlinkindRun (btn1, btn2);
 // 3. пробовал StopCoroutine ("_coroutine");
 // 4. пробовал StopCoroutine ("BlinkindRun");
  StopCoroutine (_coroutine);
}

private IEnumerator BlinkindRun (GameObject offGO, GameObject onGO) {
        while (true) {
            blinkBtn(offGO, onGO);
            yield return new WaitForSeconds (1f);

            blinkBtn(onGO, offGO);
            yield return new WaitForSeconds (1f);
        }
    }

public void blinkBtn (GameObject offGO, GameObject onGO, bool blinkGO = false) {
        offGO.SetActive (false);
        onGO.SetActive (true);
    }


// class B
private void Awake () {
        UIButton1.onClick.AddListener (onStep1Touch);
        UIButton2.onClick.AddListener (onStep2Touch);
}

private void onStep1Touch () {
  Btns.S.startFI ();
}

private void onStep2Touch () {
  Btns.S.stopFI ();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mopsicus, 2020-05-03
@mopsicus

In startFI run the coroutine twice

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question