Do-While Loop not working
I am using the While loop in the Start method. The code is as following:
for (int i = 0; i < instPointsLength; i++)
{
int ranObj = Random.Range(0, ListLength);
do
{
ranObj = Random.Range(0, ListLength);
Debug.Log(ranObj);
}
while (usedLocation.Contains(streetList[ranObj]) == true);
instPointsList[i].transform.position = streetList[ranObj].transform.position;
usedLocation.Add(streetList[ranObj]);
}
Can anyone please help me out, why Unity is freezing, maybe because it becomes an Infinity Loop, but I can't find it out how? Because the list is empty for the first FOR loop iteration, the while loop will get a false and break, and similarly, for further iterations, it should break. Am I missing something, please let me know? Thanks a lot in advance.
Answer by Perrorist · Nov 08, 2021 at 11:17 PM
If those last two statements are supposed to be in a While loop, shouldn't they be enclosed within {}?
It's okay, let me know if you find the issue. thanks for answering.
Unity is freezing completely, so nothing works, no debugs
Have you considered adding a Debug before the For loop for InstPointsLength? Or are you saying Unity freezes before it even gets that far? Does commenting out the entire statement allow Unity to continue? (I'm just trying to get a proper understanding of the problem.)
Unity freezes after pressing the Play button, it completely freezes before executing anything. And commenting that makes everything work properly.
The question must be, is it the code itself or the inclusion of a section of code. I'm still new to Unity, so I don't have the knowledge or experience to understand why Unity is freezing, but I do have many years of program$$anonymous$$g under my belt. My approach would first be to replace the code with something innocuous, like a few statements that don't affect the program. If that works, I'd then reconsider the original code and attempt to achieve the same outcome with a different formulation, i.e. by replacing the Do...While with other constructs. It's worth a try.
Your answer
Follow this Question
Related Questions
While loop freeze 0 Answers
[Solved]Cant Find loop that freezes Unity 2 Answers
Why is this Do/While loop hanging Unity? 1 Answer
While loop causing Unity to hang up? 1 Answer