Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 Tinytouchtales · Oct 22, 2013 at 06:39 PM · endless loop

While Loop within For Loop crashes Game

Hello! I want my Token spawn algorithm to not spawn more than 2 of the same type in a row. therefore i'm using a while loop within a for loop to check if the same color is within the spawn list more than 2 times.

I tried out several things but i can't see why this would produce an infinite loop:

 for (int i=0; i<_tokenList.Length; i++)
         {
             while (!canAddToken) {
             
             tokenToAdd = _tokenType[Random.Range(0,maxTokens)];
             createdTokenList.Add(tokenToAdd);
                 
             lastTokenAmount = 0;
 
             foreach (string tokenToCheck in createdTokenList)
                 {
                     if (tokenToCheck == tokenToAdd)
                     {
                         lastTokenAmount += 1;
                     }
                     
                 }
             
             if(lastTokenAmount > 2)
                 {
                     createdTokenList.Remove(tokenToAdd);
                     canAddToken = false;
                 }
             else
                 {
                     canAddToken = true;
                 }
             
             }
         
             _tokenList[i] = tokenToAdd;
             canAddToken = false;
     
         }
Comment
Add comment · Show 5
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 meat5000 ♦ · Oct 22, 2013 at 06:40 PM 0
Share

Add in some strategic Debug.Log( variable ) calls to see if its doing as you expect.

avatar image flaviusxvii · Oct 22, 2013 at 06:57 PM 0
Share

Is your goal not to have two of the same type of thing in _tokenList in a row? Cause what you are doing wouldn't solve that. Also, I find it strange that you add tokenToAdd to createdTokenList THEN you look through and compare everything in createdTokenList to tokenToAdd.. you are guaranteed 1 hit every time. It's weird.

avatar image Tinytouchtales · Oct 22, 2013 at 07:00 PM 0
Share

The goal would be to avoid more than 2 in a row. 1,1,2,2,3,1,2 would be ok, 1,1,1,2,3,2,1 would be not ok

avatar image meat5000 ♦ · Oct 22, 2013 at 07:12 PM 1
Share

A while loop can run for an indefinite amount of time. Doing this for each iteration of a for loop is very likely to grind your game to a halt unless your while loop logic is infallible.

avatar image whydoidoit · Oct 22, 2013 at 07:22 PM 0
Share

Well you aren't checking for two in a row are you? You are checking all of them - therefore if the list is long enough it will quickly get to a situation where you have two of every token and then will loop infinitely.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Oct 22, 2013 at 07:24 PM

 for (int i=0; i<_tokenList.Length; i++)
        {
          while (!canAddToken) {
  
           tokenToAdd = _tokenType[Random.Range(0,maxTokens)];
  
           canAddToken = i<2 || !(tokenList[i-1] == tokenToAdd && tokenList[i-2] == tokenToAdd);
  
          }
  
          _tokenList[i] = tokenToAdd;
          canAddToken = false;
  
        }
Comment
Add comment · Show 4 · 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 whydoidoit · Oct 22, 2013 at 07:28 PM 0
Share

$$anonymous$$ade it a bit simpler in an edit

avatar image whydoidoit · Oct 22, 2013 at 07:31 PM 0
Share

Without the loop:

    for (int i=0; i<_tokenList.Length; i++)
    {
      
       var tokenIndex = Random.Range(0,maxTokens);
       tokenToAdd = _tokenType[tokenIndex];
        

      _tokenList[i] =  i<2 || !(tokenList[i-1] == tokenToAdd && tokenList[i-2] == tokenToAdd) ? tokenToAdd : _tokenType[(tokenIndex + 1) % _tokenType.Length];

 
    }
avatar image Tinytouchtales · Oct 22, 2013 at 07:42 PM 0
Share

thanks for the rather impressive answer, but the console says: Operator ==' cannot be applied to operands of type bool' and `string'

the content of my _tokenList are actually strings.

i'm using c# btw

avatar image flaviusxvii · Oct 22, 2013 at 07:59 PM 1
Share

(tokenList[i-1] == tokenList[i-2] == tokenToAdd)

Yeah that won't work.

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

17 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

Related Questions

Need Help Ending A Game - Alphabet Game 1 Answer

Problem with while loop looping infinitely 1 Answer

How to make a road timeline look like its looping 2 Answers

Dynamically updating a Coroutine's parameters every frame 1 Answer

Endless "Importing assets" loading window in Unity editor 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