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 TheCakeMaker · Apr 16, 2014 at 02:30 PM · crashlistrandomanswers

Attempt at re-arranging list results in crash

I have a more complicated script that I've been working on. It's designed to organize the answers(dic[x,2-5]) randomly to all the questions available. It also keeps track of the correct answer(dicnum) and the secondary answer(splitnum) which is used to make a 50/50 function.

However, for some odd reason either a loop is created or some other unknown error appears and causes Unity to crash.

{for (int currentQuestion = 0; currentQuestion < 10; currentQuestion++) {

a = 0; b = 0; c = 0;

for (int j = 2; j<6; j++) {

t = (int)UnityEngine.Random.Range (2, 6);

if (t == j || t == a || t == b || t == c)

 while (t == j || t == a || t == b || t == c)

 {t = (int)UnityEngine.Random.Range (2, 6);}
                             
 variable = QuestionData.dic [currentQuestion, j];

 QuestionData.dic [currentQuestion, j] = QuestionData.dic [currentQuestion, t];

 QuestionData.dic [currentQuestion, t] = variable;

 if (QuestionData.dicnum [currentQuestion] == (j - 1))

 QuestionData.dicnum [currentQuestion] = t - 1;

 if (QuestionData.splitnum [currentQuestion] == (j - 1))

 QuestionData.splitnum [currentQuestion] = t - 1;

 if (QuestionData.dicnum [currentQuestion] == (t - 1))

     QuestionData.dicnum [currentQuestion] = j - 1;

     if (QuestionData.splitnum [currentQuestion] == (t - 1))

 QuestionData.splitnum [currentQuestion] = j - 1;

 c = b;
 b = a;
 a = t;

}}}

I've tried commenting out the if statements toward the end, which are used in order to transfer the values for the correct and secondary answers. Unity becomes functional when I comment this out, but it doesnt keep track of the correct answer anymore. Commenting out the while, which is used to ensure that no random number is selected twice, has similar results.

Anyone have any idea what is causing the crash? Any advice on how to fix it?

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 perchik · Apr 16, 2014 at 03:20 PM 0
Share

So I stripped out a lot of the code to see if I could figure out what you're doing

     int a, b, c, t;
     
     a = 0;
     b = 0;
     c = 0;
     for (int j = 2; j < 6; j++)
     {
         t = (int)UnityEngine.Random.Range(2, 6); 
       
         while (t == j || t == a || t == b || t == c)
         {
             t = (int)UnityEngine.Random.Range(2, 6);
         }
 
         c = b;
         b = a;
         a = t;
     
     }

Note, I removed the if statement before the while loop, since the while loop won't execute if that condition is false anyway (the if was redundant).

With all that said, I can't figure out what you're intending to do, and my guess is that it's hanging in that while loop (although in theory it shouldn't

avatar image TheCakeMaker · Apr 16, 2014 at 03:40 PM 0
Share

Basically what I'm ai$$anonymous$$g to do is go through the list, creating a random number for each position and swapping the two afterwards. This would ensure my list's order is random.

The list itself is a series of strings, 4 of which are the answers themselves and my goal is to randomize it so that whenever the answers are displayed the order is different.

I also have 2 other arrays which keep track of the position for the correct answer as well as a secondary answer used for a 50/50 mechanic. The if statements regarding the QuestionData are meant to update the exact position of the correct answer.

However if both the while and the ifs later on are in the code at the same time, Unity simply freezes and I am unable to do anything. Im assu$$anonymous$$g its an infinite loop, and while commenting out either while or the ifs does make it work, it also doesnt update the answer number.

1 Reply

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

Answer by perchik · Apr 16, 2014 at 03:42 PM

Found it!

Random.Range has two versions, int and float. When calling the int version (when you do Random.Range(2,6)) the upper bound is exclusive, so that means it will never return 6. Therefore, the program gets stuck in an infinite loop when a,b,c, j= (2,3,4,5) or because there's no value available for t in the range [2,6)

To fix it, either change your ranges to Random.Range(2,7) OR change it to Random.Range(2.0f, 6.0f); as the floating point version is inclusive.

My suggestion is to use the first, because then you don't have to 1.) cast a float to an int and 2.) deal with any issues arising from comparing floating point equality.

Comment
Add comment · Show 3 · 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 TheCakeMaker · Apr 16, 2014 at 03:47 PM 0
Share

are you a wizard? wow i cant believe its working now, I dont even understand.

Thank you very much!

avatar image perchik · Apr 16, 2014 at 03:52 PM 0
Share

Hah! So for record, the way I found the problem was this:

I stepped through the code I posted with monodevelop attached to unity, so I could use breakpoints to see what was going on. I had some case where a,b,c,j equaled 2,3,4,5. Obviously t needed to get set to six, but the random range wasn't generating 6.

Then I did this:

     for (int i=0; i<100000; i++) 
         {
             t = UnityEngine.Random.Range(2, 6);
             if(t==6)Debug.Log ("found 6");
         }

That never returned 6, so then I went to the documentation and saw that int Random.Range() is exclusive, not inclusive.

avatar image TheCakeMaker · Apr 16, 2014 at 03:56 PM 0
Share

I've never actually used breakpoints before, I never realized how critical to problem solving they are.

I updated my code now and everything works fine though. Thank you for your time, you were very helpful!

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

21 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

Related Questions

A node in a childnode? 1 Answer

Unity Crash on Adding to List Array, bug? 0 Answers

Choose random prefabs from list? 1 Answer

Making a camera list 1 Answer

How do I Photon Instantiate a scene object randomly from a list of objects? 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