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_.text == QuestionTb.Answers*)*_

{
//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*.colors;*

if(AnswerBoxes_.text == QuestionTb.Answers*)
{
//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*.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;
}

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.

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.