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 /
  • Help Room /
avatar image
0
Question by Reverend-Speed · Apr 11, 2016 at 08:09 AM · loopenumwhilewhile-loopwhile loop

Why is this Do/While loop hanging Unity?

Hey folks. Hoping some fresh eyes will spot the issue here. Following code hangs Unity:

 private void AssignPairs()
     {
         int currentNumberOfPairs = currentBoardSettings._posTargetsArraySize / 2;   // All the board sizes are even numbers, so can be halved to find current number of pairs
         currentPairTags     = new PairTypeTag[currentNumberOfPairs];                // Create a new array to contain this round's legitimate tags
         
         for (int i = 0; i < currentPairTags.Length; i++)
         { // Initialise the array of legitimate tags this round to EMPTY00 - blank
             currentPairTags[i] = PairTypeTag.EMPTY00;
         }
 
         for(int i = 0; i < currentPairTags.Length; i++)
         {   // Iterate through the legit tag slots...
             if(currentPairTags[i] == PairTypeTag.EMPTY00)
             {//... until you find a slot marked empty.
                 bool uniqueTag = false;    // Create a flag for the while loop, initialised to false;
 
                 do // While the flag is false, loop...
                 {
                     Debug.Log("Choosing candidate...");
                     PairTypeTag candidatePairTag = (PairTypeTag)Random.Range(1, 15);    // Choose a random tag from all the enums, excluding EMPTY00
                     Debug.Log("Looking through current pair slots...");
                     // uniqueTag = true;
                     for (int foo = 0; foo < currentPairTags.Length; i++)
                     { // Look through the array of legit tags...
                         if (candidatePairTag != currentPairTags[foo])    // If the candidate and the current legitTagSlot are NOT the same...
                         {
                             currentPairTags[foo] = candidatePairTag;    // ...assign the tag to the current legitTagSlot...
                             uniqueTag = true;                          // ...and mark the uniqueTag as true, so we don't have to loop again, hopefully not locking up.
                         }
                         else
                         {
                             Debug.Log("Failed to select pair list");
                             uniqueTag = true;
                         }
                     }
                     uniqueTag = true;
                 } while (uniqueTag == false);
                 Debug.Log("Holy shit, we got out of that While loop!");
             }
         }

I'm trying to randomly select a few enums and enter them into an array, then use this array as a source list to assign pairs of enums (0-15) to cards - basically I'm creating a Concentration game.

However, I cannot seem to stop my While and/or Do/While loops from hanging Unity! The quoted code sets uniqueTag to true no matter the outcome of the for loops (a test) and STILL the damned thing hangs.

Anybody see what I'm not seeing?

--Rev

Comment
Add comment · Show 1
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 Reverend-Speed · Apr 11, 2016 at 12:59 AM 0
Share

In testing, I commented out the most nested For loop -

 //for (int foo = 0; foo < currentPairTags.Length; i++)
                     //{ // Look through the array of legit tags...
                     //    Debug.Log("Beep three times");
                         //if (candidatePairTag != currentPairTags[foo])    // If the candidate and the current legitTagSlot are NOT the same...
                         //{
                         //    //currentpairtags[foo] = candidatepairtag;    // ...assign the tag to the current legittagslot...
                         //    uniqueTag = true;                          // ...and mark the uniquetag as true, so we don't have to loop again, hopefully not locking up.
                         //    Debug.Log("Iterating through code.");
 
                         //}
                         //    else
                         //    {
                         //        Debug.Log("Failed to select pair list");
                         //        uniqueTag = true;
                         //}
                     //}

... and the code ran without hanging. But if I reenable the For loop - even with all of its contents commented out, so it's just an empty loop - Unity hangs.

I am confused as all hell.

--Rev

1 Reply

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

Answer by Oribow · Apr 11, 2016 at 09:48 AM

In your last for loop you are incrementing the wrong value. Instead of I++, write foo++, or you getting an infinite loop.

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 Reverend-Speed · Apr 11, 2016 at 10:03 AM 0
Share

Goddamit. Correct. I am an idiot.

--Rev

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

41 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

Related Questions

NEED HELP PLEASE: When i kill a one of the three limited "mobSpawned" the "while (true)" don't work and don't spawn a new object. Anyone have a solution for this. Thanks. 1 Answer

While loop causing Unity to hang up? 1 Answer

While Loop not working when clicked on Play button 1 Answer

Why does this while loop freeze unity? 1 Answer

While loop freeze 0 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