Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Armamedia · May 30, 2018 at 03:02 PM · freezingcoroutine errors

Everything freezes on " yield return new WaitForSeconds(); " !!!

Ok! all of my code in this scene is in one script and one manager object. all of it is about 700 lines. so I can't put it here. I tested different things:

  • 1) switch platform from android to pc/mac

  • 2) test on a previous version of unity( previous 2017, and current on is 2018.1 )

none of them solve the problem. then I change some part of the code that I suspected to cause the problem. ( none of them solve the solution ). then I started to put Debug.Log()s everywhere. so I found where it freezes.

Here Is the code:

     IEnumerator ShowSigns(int Button1State, int EqualState, int Button2State)
     {

         Debug.Log("ShowSigns");
         if (Button1State == 1)
         {
             OperationOneCorrectSign.GetComponent<CanvasGroup>().alpha = 1;
         }
         else if (Button1State == 2)
         {
             OperationOneIncorrectSign.GetComponent<CanvasGroup>().alpha = 1;
         }
         if (EqualState == 1)
         {
             EqualCorrectSign.GetComponent<CanvasGroup>().alpha = 1;
         }
         else if (EqualState == 2)
         {
             EqualIncorrectSign.GetComponent<CanvasGroup>().alpha = 1;
         }
         if (Button2State == 1)
         {
             OperationTwoCorrectSign.GetComponent<CanvasGroup>().alpha = 1;
         }
         else if (Button2State == 2)
         {
             OperationTwoIncorrectSign.GetComponent<CanvasGroup>().alpha = 1;
         }

         Debug.Log("BeforeWaiting");
         yield return new WaitForSeconds(0.3f);

         Debug.Log("AfterWaiting");
         OperationOneCorrectSign.GetComponent<CanvasGroup>().alpha = 0;
         OperationOneIncorrectSign.GetComponent<CanvasGroup>().alpha = 0;
         EqualCorrectSign.GetComponent<CanvasGroup>().alpha = 0;
         EqualIncorrectSign.GetComponent<CanvasGroup>().alpha = 0;
         OperationTwoCorrectSign.GetComponent<CanvasGroup>().alpha = 0;
         OperationTwoIncorrectSign.GetComponent<CanvasGroup>().alpha = 0;
         state = GameState.CreateNewProblem;

         Debug.Log("EndSigns");
     }

I found that it freezes on this:

         yield return new WaitForSeconds(0.3f);

Very strange!!!

This is a picture of the game.

alt text

The game is a simple game that shows 2 math phrase and player should choose the bigger or equal. The logic is this way:

  • 1) make new phrases and change the game state to "ChooseAnswer"

  • 2) player press one of 3 buttons and the answer checked and score and other things changes and the ShowSigns coroutine will start and ends after 0.3 seconds. and as you see at the end of the coroutine state changes to "CreateNewProblem".

  • 3) in the Update when CreateNewProblem detects, the code call for the NewProblem() function to make new phrases and at the end of that game state changes to "ChooseAnswer".

this logic repeats over and over until time reaches zero. a "step" variable increase and decrease by 1 by any correct and incorrect answer. and a variable level = steps/10 determines the difficulty of phrases.

the game works correctly on %98 click On buttons. but usually, it freezes somewhere after step 20. In 21, 23, 27, 34 ... very randomly. but always after 20 and some time no freeze until time ends. and always right before yield return. exactly at the same line.

I read many questions and answers but none of them was helpful. I have no while loop, no while(true), as long as I know and check my code no infinite loop, on StopAllCoroutines ... nothing. and I stuck for 2 days. thanks all of you for helping. Oh, and here is the code file. I just change the format to .txt

link text

game3manager.txt (32.2 kB)
qeqwqeeqwe.jpg (463.6 kB)
Comment
Add comment · Show 2
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 Prastiwar · May 30, 2018 at 03:44 PM 0
Share

You can put code in https://hastebin.com/ or https://gist.github.com/
You can also report bug to Unity https://unity3d.com/unity/qa/bug-reporting

avatar image Bunny83 · May 30, 2018 at 10:25 PM 0
Share

Your code is pure Spaghetti code. It's really hard to actually follow the control flow. You really should split up your code into logical sections and use more descriptive variables and method names. Also you seem to use the wrong Random.Range limits in almost all cases. When you use the integer version of Random.Range the upper limit is exclusive. So Random.Range(1,2) will always return "1". Random.Range(1,9) will return a value between 1 and 8 inclusive but never 9

3 Replies

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

Answer by Armamedia · May 31, 2018 at 01:53 PM

OK, I tried some math equations in different difficulty types instead of those while loops. Check the code file that I attached. In GeneratePhrase() method. The actual Problem was there. And now it works perfectly. No freezing anymore. And even I get better Phrases now. and I still use that while loop inside CountDown coroutine and have no problem. ( using while loops is Ok. in another forum someone said "never use while loops in unity. while loops in unity always cause problems." wrong! ) take a look at this link for more info


And probably the problem was using a RandomRange to tell the code that when it must exit the while loop. I suspected that Random.Range() could be the problem but it usually stayed in while loop less than 4 times looping. so I ignore that it can be stuck there in case of a very bad luck.

unfortunately, in coding, the bad luck always occurs.

Comment
Add comment · 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
0

Answer by Legend_Bacon · May 30, 2018 at 03:54 PM

Hello there,

After checking your code out a little bit, I don't see anything that would cause this issue. My suggestions would be:

• When "it freezes", do you mean the Unity Editor or the game? Can you still use Unity or do you have to go to the task manager and manually terminate it?

• If it's just the game that freezes, then my guess would be that somehow your timescale gets set to 0 somewhere. That means the WaitForSeconds() never ends. Could you try replacing it with WaitForSecondsRealtime() and see if it helps?

• Also, kudos on actually using coroutine assigning to start and stop them properly. Not a lot of people do that.


I hope that helps!

Cheers,

~LegendBacon

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 Armamedia · May 30, 2018 at 04:29 PM 0
Share

thank you.

  • first, the game and the editor both freezes and I have to go to the task manager and manually ter$$anonymous$$ate it. yes.

  • second I never change timescale in my code. and is it possible that it changed to 0 by itself, somehow?!

  • third that yes I stop the ShowSigns() coroutine every time after starting and the only coroutine that acts continuously is the CountDown().

the only solution that I found was to remove while loops and don't use them and strangely solves the problem. he sais using whiles in unity always cause problems that mostly are untrackable. but coding without while loops could be very painful in some cases. I'm looking for a better solution. at least a good alternative to while loops.

avatar image
0

Answer by jim3878 · May 30, 2018 at 04:37 PM

mm....you start ShowSign by operation1Button, operation2Button, and equalButton ,but only set state to GameState.CheckingAnswer(line 129) in operation1Button.

i think you should change to

             operation1Button.onClick.AddListener(() =>
             {
                 Debug.Log("ChooseAnswer");
                 if ( state == GameState.ChooseAnswer)
                 {
                     state = GameState.CheckingAnswer;
                     //....
                 }
             });
             equalButton.onClick.AddListener(() =>
             {
                 Debug.Log("ChooseAnswer");
                 if (state == GameState.ChooseAnswer)
                 {
                     state = GameState.CheckingAnswer;
                     //....
                 }
             });
             operation2Button.onClick.AddListener(() =>
             {
                 Debug.Log("ChooseAnswer");
                 if (state == GameState.ChooseAnswer)
                 {
                     state = GameState.CheckingAnswer;
                     //....
                 }
             });

hope that helps

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 Armamedia · May 30, 2018 at 05:21 PM 0
Share

thank you for that close looking at my code. but I change it, correct it. but still, have problem.

avatar image jim3878 Armamedia · May 30, 2018 at 05:47 PM 1
Share

i guest problem is on your while loop

 while (result < 1 || result > 50 || result < (recommendedNumber - 2) || result > (recommendedNumber + 2))
                             {
                                 numberOne = UnityEngine.Random.Range(1, 9);
                                 numberTwo = UnityEngine.Random.Range(1, 9);
                                 result = numberOne + numberTwo;
                             }

or

 while (result < 1 || result > 50 || result < (recommendedNumber - 2) || result > (recommendedNumber + 2))
                             {
                                 numberOne = UnityEngine.Random.Range(1, 9);
                                 numberTwo = UnityEngine.Random.Range(1, 9);
                                 result = numberOne * numberTwo;
                             }

I suggest adding a counter to break it like this

 int count=0;                        
 while (count<100&&( result < 1 || result > 50 || result < (recommendedNumber - 2) || result > (recommendedNumber + 2)))
                         {
                             count++;
                             numberOne = UnityEngine.Random.Range(1, 9);
                             numberTwo = UnityEngine.Random.Range(1, 9);
                             result = numberOne * numberTwo;
                         }
 if(count==100){
 throw new Exception("while is broken.");
 }




i am not sure if it is Infinite Loop because i dont know what your recommendedNumber mean

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

86 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 avatar image avatar image

Related Questions

Weapon Movement Freezing 0 Answers

Coroutine crashes game 2 Answers

Unity (2018.2f and 5.6) Freezes when using While Loop for countdown timer in coroutine. 3 Answers

Freeze when building 0 Answers

Coroutine making unity freeze 2 Answers


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