Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sadowlight123 · Apr 14, 2018 at 07:38 AM · coroutinetimepause

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;
 }





Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sadowlight123 · Apr 14, 2018 at 08:13 AM 0
Share

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
 
         }
 
     }

avatar image Chronos-L sadowlight123 · Apr 14, 2018 at 08:25 AM 1
Share

it ends the coroutine. it is optional. I wrote it out of habit

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sadowlight123 · Apr 14, 2018 at 08:17 AM 0
Share

thank you , i will make sure to go google more about it :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

84 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pause menu... Isn't pausing everything... 1 Answer

Timecale=0 stops joystick. can it not? 1 Answer

How to stop Audio Track in Timeline? 1 Answer

Use for-i-loop var outside the loop 2 Answers

Couldn't resume once Time.timeScale is set to 0 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges