Search code examples
c#unity-game-engineonclickgame-developmentunity-ui

Trying to increment a variable in a Coroutine on pressing a UI button


I am making a simple quiz game for children. In which i am extracting questions from a json file. Each question will have a 10 second timer and if the button is pressed next question is displayed. the problem i am facing is that i can't seem to figure out a way to control "yeild return waitforseconds()". i have tried multiple approaches but none seems to work, it either skips the next question too fast if i try to change the value in waitforseconds(), or it gets stuck on the same question if try incrementing the questionCounter variable. I have been searching for a while a but i can't find anything. i have a deadline of tomorrow, i could really use some help.

 //this is my question changing method
  IEnumerator QuestionRoutin(Course c, int n)
     {
         while (qCounter < 3)
         {

             print(qCounter);
             r = UnityEngine.Random.Range(0, 6);
             //Question.text = c.Ques_Data[r].Quesion;
             //RandomButton(c, r);
             //amil = c.Ques_Data[r].Answer;

             if(checkq.Count==0)
             {

                 checkq.Add(r);
                 Question.text = c.Ques_Data[r].Quesion;
                 RandomButton(c, r);
                 amil = c.Ques_Data[r].Answer;
             }

             else if (checkq.Contains(r)==false)
             {

                 checkq.Add(r);
                 Question.text = c.Ques_Data[r].Quesion;
                 RandomButton(c, r);
                 amil = c.Ques_Data[r].Answer;
             }
             else
             {

                 continue;
             }

             if (f == true) //f is a global bool
             {
                f = false;
                qCounter++;
                //yield return new WaitForSeconds(1.0f);
             }
             else
             {

                 yield return new WaitForSeconds(10);
                 qCounter++;
             }

         }

         print("questions are finished");

     }



  //this a function attach to my buttons
  public void CheckAnswer(Text button_txt)
 {
     //time_1 = 10.0f;
     if (button_txt.text == amil)
     {
         flag = true;

         Animator anim = Correct_Panel.GetComponentInChildren<Animator>();
         if (anim != null)
         {

             Correct_Panel.SetActive(true);
             bool isRightAnsPressed = anim.GetBool("isRightAnsPressed");
             anim.SetBool("isRightAnsPressed", !isRightAnsPressed);
             Invoke("disableCorrectPanel", 1.0f);
             score_value = score_value + 10;
             score.text = "Score: " + score_value;

 //have tried all these 3 things
             //StartQuestionRoutine(c1, n,true);
             //qCounter++;
            // flag = true;
         }
 }

now ik you,ll say coroutine will restart again if i call it in the button, but nothing else seems to work. how else will ik if my button is pressed or not? Please i could really use some help.


Solution

  • have you try to stop coroutine before to relaunch it:

    private IEnumerator coroutine;
    
    
    :
    :
     if(coroutine != null) StopCoroutine(coroutine);
     coroutine = StartCoroutine(QuestionRoutin(.....));