Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Kerihobo · Nov 26, 2014 at 10:14 PM · loopfreezewhile

while loops infinitely, I can't tell why.

The way I expect this to work, is that the while loop will trigger this action, and if it makes it to the end with nothing happening, it should go through that same iteration again to roll a new random number to hopefully pass it's check this time, only THEN will i++ and proceed to the next iteration.

This works up until 2 of my targets have 0 health... then it starts looping infinitely and unity freezes... why? Shouldn't it just continue trying to roll "a" until "a" is a target with more than 0 health (as a side note, there are 6 targets, the infinite-looping begins after I've killed 2 of them usually...)?

 void SelectFour ()
 {    
     while (int i = 0; i < 4)
     {
         int a = Random.Range (0, 5);
         
         if (targetHealth[a]> 0)
         {
             DoSomething();    
             i++;
         }
     }
 }

Comment
Add comment · Show 4
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 Uldeim · Nov 26, 2014 at 10:53 PM 0
Share

You can't declare a variable in a while loop; it declares a new instance every time. $$anonymous$$ove the declaration outside.

avatar image Kerihobo · Nov 26, 2014 at 11:05 PM 0
Share

so more like????:

 void SelectFour ()
 {
     int i = 0;
 
     while (i < 4)
     {
         int a = Random.Range (0, 5);
         
         if (targetHealth[a]> 0)
         {
             DoSomething();    
             i++;
         }
     }
 }

???

avatar image Uldeim · Nov 26, 2014 at 11:07 PM 0
Share

Yes, indeed.

avatar image Kerihobo · Nov 26, 2014 at 11:16 PM 0
Share

Thanks man, I'll give that a try when I get home.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Uldeim · Nov 26, 2014 at 10:46 PM

While I can't speak to the exact problem, since the code for how targetHealth is populated isn't there, I would suggest against using this approach, if possible.

The issue is that you have no control over how many times the loop will execute; you're hoping the random numbers eventually hit something that works, so this can theoretically never complete even in a mostly good case.

What I would recommend instead is to generate a collection of all possible valid targets, then generate a random number within the length of that set.

Something like (I'm guessing a lot about how things work):

 List<BodyPart> validTargets = new List<BodyPart>();
 for (int i = 0; i < bodyParts.Length; i++)
 {
    if (targetHealth[i] > 0)
      validTargets.Add(bodyParts[i]);
 }
 
 if (validTargets.Count > 0)
 {
   for (int i = 0; i < 4; i++)
   {
     int a = Random.Range(0, validTargets.Count);
     DoSomething(validTargets[a]);
   }
 }
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 Kerihobo · Nov 27, 2014 at 12:19 AM 0
Share

I like where this is going, I'll have to try it out when I get home. I see that part of my problem has been that I have been trying to use arrays in a very dynamic way, but my life could be made simpler by using lists, problem there is I've never used lists before, I'll give this a go when I get home.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity crashes when using while 2 Answers

Breaking out of a loop 2 Answers

Help With "For Loop" Not Working? 2 Answers

Increment in while loop, with timeout 1 Answer

How to wait for Coroutine to finish? 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