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 /
This question was closed Sep 21, 2016 at 09:18 PM by ZombyKillr for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ZombyKillr · Sep 21, 2016 at 07:02 PM · errorinfinitewhile loop

Potential infinite while loop?

This code assigns each of the four players a secret role, as well as three fake roles. However, sometimes I click play, and the scene does not open. The play button is still grey, but it will not turn blue to load the scene. I looked it up and the main answer I got was that it could be an infinite while loop. Below is my code which occurs in the start function.

     p1_role = Random.Range(1, 10);
     p2_role = Random.Range(1, 10);
     p3_role = Random.Range(1, 10);
     p4_role = Random.Range(1, 10);


     // prevent repeats of roles

     while (p2_role == p1_role)
     {
         p2_role = Random.Range(1, 10);
     }

     while (p3_role == p1_role || p3_role == p2_role)
     {
         p3_role = Random.Range(1, 10);
     }

     while (p4_role == p1_role || p4_role == p2_role || p4_role == p3_role)
     {
         p4_role = Random.Range(1, 10);
     }


The code also assigns three fake roles per player, that must not match the real role:

     p1_fake_role_1 = Random.Range(1, 10);
     while (p1_fake_role_1 == p1_role)
     {
         p1_fake_role_1 = Random.Range(1, 10);
     }
     p1_fake_role_2 = Random.Range(1, 10);
     while (p1_fake_role_2 == p1_fake_role_1 || p1_fake_role_2 == p1_role)
     {
         p1_fake_role_1 = Random.Range(1, 10);
     }

     p1_fake_role_3 = Random.Range(1, 10);
     while (p1_fake_role_3 == p1_fake_role_2 || p1_fake_role_3 == p1_fake_role_1 || p1_fake_role_3 == p1_role)
     {
         p1_fake_role_3 = Random.Range(1, 10);
     }


This fake role assigning is repeated for each player. The strange thing is, the scene runs totally fine sometimes, but other times it won't open, as I described above.

Big thanks in advance for any help!

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 gjf · Sep 21, 2016 at 05:08 PM 1
Share

there's a simpler approach to this problem.

create a list with all of the possible roles then create a method to assign roles from that list. after assigning a role, remove it from the list of available roles. that way you will never assign the same role to another player and you won't need those messy while statements...

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Bonfire-Boy · Sep 21, 2016 at 09:08 PM

Yes, it's an infinite loop. On line 9 of the second script you assign to p1_fake_role_1 instead of p1_fake_role_2. So sometimes it'll get trapped there.

But gjf is right, there are much better ways to do this. You're creating a lot of work for yourself with this approach - not only in the coding (so much code to add every time you change your set of roles) but also because you're much more likely to end up with bugs.

Think of your set of roles like a pack of cards. Put them in a list, shuffle it, then just pop them from the list as required.

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 ZombyKillr · Sep 21, 2016 at 09:17 PM 0
Share

Oh god, you're right...I can't believe I missed that. Thanks! I'm trying out using lists ins$$anonymous$$d as well, and it seems to be working.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Objects wont stop bouncing if they fall. 0 Answers

Why is this making an infinite loop? 2 Answers

Why Do I keep Getting An Infinite Loop? Speech Dialogue 0 Answers

Delete duplicates in a list of vectors 1 Answer

Raycast.distance not working in a while loop 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