- Home /
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?
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
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.
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.
are you a wizard? wow i cant believe its working now, I dont even understand.
Thank you very much!
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.
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
Follow this Question
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