Search Unity

Do while makes unity crash.

Discussion in 'Scripting' started by casiantumblr, Apr 5, 2018.

  1. casiantumblr

    casiantumblr

    Joined:
    Mar 20, 2018
    Posts:
    4
    Hello, I was writing some simple code then I went through something strange...
    My unity editor crashes just if I enter play mode with the 2 incriminated lines in void Update(), that I've signed as a comment, are active. Why should a simple do while making unity crash?
    I've even tried other forms like while (!Ewall) but nothing changes.
    Any kind of help would be appreciated, thank you.
    Code (CSharp):
    1. bool Ewall=false;
    2.  
    3.     void Start(){
    4.     }
    5.  
    6.     void Update ()
    7.     {
    8.        
    9.             var dir = Vector3.zero;
    10.  
    11.             if (Input.GetKey (KeyCode.UpArrow))
    12.                 dir = Vector3.forward;
    13.    
    14.             if (Input.GetKey (KeyCode.DownArrow))
    15.                 dir = Vector3.back;
    16.  
    17.             if (Input.GetKey (KeyCode.LeftArrow))
    18.                 dir = Vector3.left;
    19.         //do {
    20.             if (Input.GetKey (KeyCode.RightArrow))
    21.                 dir = Vector3.right;
    22.         //} while(Ewall==false);
    23.             if (dir != Vector3.zero && !isTumbling) {
    24.                 StartCoroutine (Tumble (dir));
    25.             }  
    26.     }
    27.  
    28. void OnTriggerEnter(Collider other){
    29.         if (other.gameObject.CompareTag ("Ewall")) {
    30.             Ewall = true;
    31.             Debug.Log ("EastWall!");
    32.         }
    33.     }
    34. }
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,501
    A while loop will tie up the process indefinitely until the condition no longer resolves to true. Since you aren't changing the condition inside the while loop, it can never exit the loop.

    Update() is called one per frame, so you don't need a while loop.
     
    Ryiah and casiantumblr like this.
  3. casiantumblr

    casiantumblr

    Joined:
    Mar 20, 2018
    Posts:
    4
    Thank you, problem solved, but I still can't understand why unity crashes instead of reporting an error or something... It has to be fixed.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,501
    It's your fault for writing code that causes the program to be literally be stuck in an infinite loop. This is a standard thing that you're expected to avoid doing as a programmer. Unity can't make you not make bad decisions.
     
    casiantumblr likes this.
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    FYI, in some cases you can avoid the crash. When you notice that Unity appears to have locked up, and you suspect it's your code, you can attach the debugger and put a breakpoint within your loop. In some cases you can modify the values in the debugger, causing the loop to end. It's not always feasible, but I've prevented crashes this way before.
     
    casiantumblr likes this.