Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help pls. I have an endless while loop and I dont know why.

Discussion in '2D' started by drskort, Oct 15, 2015.

  1. drskort

    drskort

    Joined:
    Oct 15, 2015
    Posts:
    2
    This is my LevelGenerator and please tell me, what I missed in the Code :
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. import System.Collections.Generic;
    4.  
    5. var Feld : GameObject;
    6. var Point : GameObject;
    7. var List : List.<GameObject>;
    8. var c : int;
    9. var Direction : float;
    10. var Test : int = 0;
    11. var Test2 : String = "";
    12.  
    13. function Start () {
    14.  
    15.  
    16. List.Add(Point); // List 1
    17.  
    18. var ListLength : int = 1; // ListLength 1
    19. for (var i : int = 0; i<20; i++)
    20. {
    21.  
    22.  
    23. Instantiate(Feld); // Feld
    24. c = 0; // c 0
    25. do{
    26. Direction = Random.Range(0,4);
    27. Test = 0;
    28. c = 0; // jedes mal ein Reset c
    29. if (Direction < 1 ){
    30. Feld.transform.localPosition = Vector3(Point.transform.localPosition.x,Point.transform.localPosition.y + 10,Point.transform.localPosition.z);
    31. }else if (Direction < 2){
    32. Feld.transform.localPosition = Vector3(Point.transform.localPosition.x,Point.transform.localPosition.y - 10,Point.transform.localPosition.z);
    33. }else if (Direction < 3){
    34. Feld.transform.localPosition = Vector3(Point.transform.localPosition.x - 10,Point.transform.localPosition.y,Point.transform.localPosition.z);
    35. }else if (Direction < 4){
    36. Feld.transform.localPosition = Vector3(Point.transform.localPosition.x + 10,Point.transform.localPosition.y,Point.transform.localPosition.z);
    37. }
    38.  
    39.  
    40.  
    41. Test = ListLength;
    42.  
    43. for (var i2 : int = 0; i2 < ListLength;  i2++)
    44. {
    45. if (Feld.transform.localPosition.x == List[i2].transform.localPosition.x && Feld.transform.localPosition.y == List[i2].transform.localPosition.y){
    46. c = 1;
    47. }
    48.  
    49. }
    50.  
    51. } while(c == 1);
    52.  
    53. Point.transform.localPosition.x = Feld.transform.localPosition.x;
    54. Point.transform.localPosition.y = Feld.transform.localPosition.y;
    55.  
    56. List.Add(Feld);
    57.  
    58. ListLength = ListLength + 1;
    59.  
    60. }
    61.  
    62. }
     
    Last edited: Oct 15, 2015
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  3. drskort

    drskort

    Joined:
    Oct 15, 2015
    Posts:
    2
    Sry, I edited it.
     
    Last edited: Oct 15, 2015
  4. Unexpected_Persona

    Unexpected_Persona

    Joined:
    Jul 23, 2015
    Posts:
    9
    On line 46 you set c as 1 and on line 51 you have a while c == 1 without ever changing c from 1. Not sure if this is causing the problem.
     
  5. Tutelage Systems

    Tutelage Systems

    Joined:
    Jun 7, 2013
    Posts:
    6
    Don't be afraid to put some debug lines in your code to step through it.

    We can't really see everything that is happening, but I am assuming that you set c = 0; at the start. When it goes into your while loop and it steps through the for statement the first (and only) if statement at some point becomes true. This causes C becomes 1 and it never gets changed again back to anything else, thus an infinite loop will occur.

    For whatever reason when you are looping through your list, that if statement is becoming true at least once (always) and sets C to 1;
     
  6. Griftenatt

    Griftenatt

    Joined:
    Mar 27, 2015
    Posts:
    15
    C is getting the value of 1 and never change, thats what cause the infinit loop.