- Home /
stop game for 1 second
Hello,
I am trying to go through a list of inputs (answers) and i need to give score to user for them . However I am not sure how I can do it without having the game going crazy.
It is not working here , probably because im blocking the main thread i guess...
The problem of interest is mainly here :
  if(AnswerBoxes[i].text == QuestionTb.Answers[i])
         {
             //add score
             GameController.Instance.AddScore(10);
             //add sound effect
             SFXController.Instance.SetAudioSrc(SFXController.Instance.CorrectSFX);
             //show positive character
             GameController.Instance.ShowCharacter(true);
             //color it in a positive way
             cb.disabledColor = CorrectColor;
             //wait for half a second
             float counter = 2f;
             while (counter > 0)
                 counter -= Time.deltaTime;
             //hide positive character
             GameController.Instance.HideCharacter();
         }
as you see I am trying to do the following:
loop through all answers
if answer is correct :
-play correct sound
-add score
-show a quick animation to praise player
-color the input field with green to show correct
-give player 1 second to see all of this
-stop the animation
-move to next answer
the part of give player 1 second is messing up everything . How can I achieve the effect I want in a better manner?
can anyone help me please?
//this helps to check the list of answers
 public void CheckAnswer()
 {
     int n = QuestionTb.Answers.Count;
   
     for (int i = 0; i < n; i++)
     {
        ColorBlock cb = AnswerBoxes[i].colors;
       
     if(AnswerBoxes[i].text == QuestionTb.Answers[i])
     {
         //add score
         GameController.Instance.AddScore(10);
         //add sound effect
         SFXController.Instance.SetAudioSrc(SFXController.Instance.CorrectSFX);
         //show positive character
         GameController.Instance.ShowCharacter(true);
         //color it in a positive way
         cb.disabledColor = CorrectColor;
         //wait for half a second
         float counter = 2f;
         while (counter > 0)
             counter -= Time.deltaTime;
         //hide positive character
         GameController.Instance.HideCharacter();
     }
     else
     {
         //deduce score
         GameController.Instance.OmitScore(3);
         //add sound effect
         SFXController.Instance.SetAudioSrc(SFXController.Instance.WrongSFX);
         //show negative character
         GameController.Instance.ShowCharacter(false);
         //color it in a negative way
         cb.disabledColor = WrongColor;
         //wait for half a second
         float counter = 2f;
         while (counter > 0)
             counter -= Time.deltaTime;
         //hide negative character
         GameController.Instance.HideCharacter();
     }
     AnswerBoxes[i].colors = cb;
     }
 }
this is called from a button in the scene
 public void ButtonCheck()
 {
 
     CheckAnswerBtn.interactable = false;
     foreach(InputField inp in AnswerBoxes)
     {
         inp.interactable = false;
     }
     CheckAnswer();
     HoldIt = true;
 }
Answer by Chronos-L · Apr 14, 2018 at 08:02 AM
You need to make use of a coroutine in order for to wait for 1 second.
 public void ButtonCheck()
 {
     ...
     StartCoroutine(CheckAnswer());
     ...
 }
 IEnumerator CheckAnswer()
 {
     ...
     yield return new WaitForSeconds(1f);
     ...
     yield return null;
 }
This chunk of code does not work because the loop is completed at the same frame it is called.
      float counter = 2f;
      while (counter > 0)
          counter -= Time.deltaTime;
It will work the way you intended if you put it in a coroutine and use WaitForEndOfFrame() like so
  while(counter > 0f)
  {
      counter -= Time.deltaTime;
      yield return new WaitForEndOfFrame();
  }
If you don't need to use counter during the loop in other calculations, like doing a Mathf.Lerp. Just use yield return new WaitForSeconds(1f) to make the coroutine wait for 1 second before proceeding. 
Thank you for the great explanation.
$$anonymous$$ay I ask one little additional question please? I do not understand where the
  yield return null;
goes or what it does
 IEnumerator CheckAnswer()
     {
         // stuff
         for (int i = 0; i < n; i++)
         {
             //stuff
 
             if (AnswerBoxes[i].text == QuestionTb.Answers[i])
             {
                 //stuff
                 yield return new WaitForSeconds(1f);
                //stuff
             }
             else
             {
                 //stuff
                 yield return new WaitForSeconds(1f);
                //stuff
             }
             /stuff
 
         }
 
     }
it ends the coroutine. it is optional. I wrote it out of habit
Answer by MarioSantoso · Apr 14, 2018 at 07:56 AM
Your program was stuck at the while loop. It cannot continue to the next line until you finish the while loop.
What you want to achieve is very common, and can be achieved by using Invoke or IEnumerator.
I suggest you take a look at Unity documents for those functions.
thank you , i will make sure to go google more about it :D
Your answer
 
 
             Follow this Question
Related Questions
Pause Game When Function Is Enabled? 3 Answers
Pause and resume coroutine 0 Answers
OnCollisionEnter as IEnumerator problem (WaitForSecond) 2 Answers
Stop force from being applied during pausescreen 1 Answer
Use for-i-loop var outside the loop 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                