C# 尝试在按下UI按钮时增加协同程序中的变量

C# 尝试在按下UI按钮时增加协同程序中的变量,c#,unity3d,onclick,game-development,unity-ui,C#,Unity3d,Onclick,Game Development,Unity Ui,我正在为孩子们做一个简单的智力竞赛游戏。我从一个json文件中提取问题。每个问题都有一个10秒的计时器,如果按下按钮,将显示下一个问题。我面临的问题是,我似乎无法找到控制“yeild return waitforseconds()”的方法。我尝试了多种方法,但似乎都不起作用。如果我尝试更改waitforseconds()中的值,它会过快跳过下一个问题,或者如果尝试递增questionCounter变量,它会陷入同一个问题。我已经找了一段时间了,但什么也找不到。明天是我的最后期限,我真的需要一些帮

我正在为孩子们做一个简单的智力竞赛游戏。我从一个json文件中提取问题。每个问题都有一个10秒的计时器,如果按下按钮,将显示下一个问题。我面临的问题是,我似乎无法找到控制“yeild return waitforseconds()”的方法。我尝试了多种方法,但似乎都不起作用。如果我尝试更改waitforseconds()中的值,它会过快跳过下一个问题,或者如果尝试递增questionCounter变量,它会陷入同一个问题。我已经找了一段时间了,但什么也找不到。明天是我的最后期限,我真的需要一些帮助

 //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;
         }
 }
//这是我的问题转换方法
IEnumerator问题解答(课程c,国际n)
{
而(qCounter<3)
{
打印(qCounter);
r=单位发动机随机范围(0,6);
//Question.text=c.Ques_Data[r].Quesion;
//随机按钮(c,r);
//amil=c.Ques_Data[r]。答案;
if(checkq.Count==0)
{
检查q.添加(r);
Question.text=c.Ques_Data[r].Quesion;
随机按钮(c,r);
amil=c.Ques_Data[r]。答案;
}
else if(checkq.Contains(r)=false)
{
检查q.添加(r);
Question.text=c.Ques_Data[r].Quesion;
随机按钮(c,r);
amil=c.Ques_Data[r]。答案;
}
其他的
{
继续;
}
如果(f==true)//f是全局布尔
{
f=假;
qCounter++;
//收益率返回新的WaitForSeconds(1.0f);
}
其他的
{
产生返回新WaitForSeconds(10);
qCounter++;
}
}
打印(“问题已完成”);
}
//这是一个附加到我的按钮上的功能
公共无效检查应答(文本按钮)
{
//时间_1=10.0f;
if(button_txt.text==amil)
{
flag=true;
Animator anim=正确的_面板.getComponentChildren();
如果(动画!=null)
{
正确的面板设置激活(真);
bool isrightranspressed=anim.GetBool(“isrightranspressed”);
动画设置工具(“isRightAnsPressed”,!isRightAnsPressed);
调用(“disableCorrectPanel”,1.0f);
分值=分值+10;
score.text=“score:+score\u值;
//我试过这三种方法
//开始查询例程(c1,n,真);
//qCounter++;
//flag=true;
}
}

现在,我告诉你,如果我在按钮中调用协同程序,它将再次重新启动,但其他功能似乎都不起作用。如果我的按钮被按下或未按下,ik将如何运行?请告诉我,我真的需要一些帮助。

在重新启动它之前,你是否尝试过停止协同程序:

 //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;
         }
 }
private IEnumerator coroutine;


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

在重新启动协同程序之前,您是否尝试停止协同程序:

private IEnumerator coroutine;


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

谢谢老兄。这就是我所需要的。我停止了例行程序,然后重新开始。谢谢老兄。这就是我所需要的。我停止例行程序,然后重新开始。