How to interrupt wait coroutine in a loop?
Hi, I am working on a basic game and I stuck. The idea is creating a random numbers array and printing numbers to the screen and asking the user to click buttons according to the rule. For example the rule may be: click the left button if the number is even and click the right button if the number is odd. User got 2 seconds to answer for each number and after two seconds the next number prints. If the user clicks a button before 2 seconds the answer is saved in an array and next number prints(without waiting 2 seconds to complete). The problem is I could not implement a wait coroutine with cancelWait variable in a loop. It looks like in the loop all coroutines are fired simultaniously for some reason . Here is my code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DemoGM : MonoBehaviour {
 
     int[] numberArray = new int[10];
     public Text mainText;
     private float waitSystem;
 
     // Use this for initialization
     void Start () {
         for (int i = 0; i < 10; i++) {
             numberArray[i] = Random.Range(0, 10);
             print(numberArray[i]);
         }
         StartGame();
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     void StartGame() {
         StartCoroutine(PrintNumbers());
     }
 
     IEnumerator PrintNumbers() {
 
         mainText.text = "Rule";
         yield return new WaitForSeconds(3);
         for (int i = 0; i < 10; i++) {
             mainText.text = numberArray[i].ToString();
             StartCoroutine(WaitForSecondsOrTap(3));
         }
         
     }
 
     public void OddButtonClicked() {
         //Save Answer
         cancelWait();
     }
     public void EvenButtonClicked()
     {
         //Save Answer
         cancelWait();
     }
 
 
     IEnumerator WaitForSecondsOrTap(float seconds)
     {
         waitSystem = seconds;
         while (waitSystem > 0.0)
         {
             waitSystem -= Time.deltaTime;
             yield return 0;
         }
     }
     private void cancelWait()
     {
         waitSystem = 0.0f;
     }
 }
 
Any help appreciated.
Your answer
 
 
             Follow this Question
Related Questions
Fade out / fade in scene w/ loading screen 0 Answers
Unity MVC animation coroutine 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                