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
  4. Dismiss Notice

Unity crashes when a while starts

Discussion in 'Scripting' started by HiperGlue, Dec 18, 2020.

  1. HiperGlue

    HiperGlue

    Joined:
    Dec 18, 2020
    Posts:
    1
    Hello! First, i'm new in C# and Unity so, maybe i don't see why this is happening when the solution is in front of me...

    Ok, when i go to previsualize my project Unity crashes, and this is happening (i suppose) because Unity starts a while. And i can imagine that this isn't normal. I don't if the code i write have sense or no, but i suppose the code is normal.
    So, the question is here, it's my pc the problem or it's the code?

    Thanks in advance for anyone can tell me the solution and sorry for my really bad English.

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if(Input.GetKeyDown(KeyCode.W))
    4.         {
    5.             if (canMoveW == true)
    6.             {
    7.                 Pos = transform.position;
    8.                 Instantiate(pTail, new Vector3(Pos.x, Pos.y, Pos.z), Quaternion.identity);
    9.                 while(transform.position.y != Pos.y + 1)
    10.                 {
    11.                     transform.Translate(0, moveSpeed, 0);
    12.                 }
    13.                 StartCoroutine(WaitForMove(WaitSpeed));
    14.             }
    15.         }
    16.         if (Input.GetKeyDown(KeyCode.S))
    17.         {
    18.             if (canMoveS == true)
    19.             {
    20.                 Pos = transform.position;
    21.                 Instantiate(pTail, new Vector3(Pos.x, Pos.y, Pos.z), Quaternion.identity);
    22.                 while (transform.position.y != Pos.y + 1)
    23.                 {
    24.                     transform.Translate(0, -moveSpeed, 0);
    25.                 }
    26.                 StartCoroutine(WaitForMove(WaitSpeed));
    27.             }
    28.         }
    29.         if (Input.GetKeyDown(KeyCode.A))
    30.         {
    31.             if (canMoveA == true)
    32.             {
    33.                 Pos = transform.position;
    34.                 Instantiate(pTail, new Vector3(Pos.x, Pos.y, Pos.z), Quaternion.identity);
    35.                 while (transform.position.y != Pos.y + 1)
    36.                 {
    37.                     transform.Translate(-moveSpeed, 0, 0);
    38.                 }
    39.                 StartCoroutine(WaitForMove(WaitSpeed));
    40.             }
    41.         }
    42.         if (Input.GetKeyDown(KeyCode.D))
    43.         {
    44.             if (canMoveD == true)
    45.             {
    46.                 Pos = transform.position;
    47.                 Instantiate(pTail, new Vector3(Pos.x, Pos.y, Pos.z), Quaternion.identity);
    48.                 while (transform.position.y != Pos.y + 1)
    49.                 {
    50.                     transform.Translate(moveSpeed, 0, 0);
    51.                 }
    52.                 StartCoroutine(WaitForMove(WaitSpeed));
    53.             }
    54.         }
    55.     }
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    you're creating infinite loops

    1) a while loop to move something won't occur over time. It'll all happen in the moment. You should look into coroutines if you want to move things over a series of frames.

    2) you're using float comparison to end your while loop. floats are inherently bad at comparison... checking if something != Pos.y + 1, well it's very likely it'll always never equal that because of float error. You could easily end up at Pos.y + 1.000001 instead.

    You should google about float error, heck if you google just "unity float error" you'll get a bunch of posts where people are like "UNITY BUG NUMBERS WRONG" and then others going "nope, that's just the way floats work... it's inherent to how computers work".
     
    Kurt-Dekker, SparrowGS, Havyx and 2 others like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    This ^^^ don't do that in Unity. It's not how modern event-driven apps work, generally.

    Always yield in a coroutine if you want to do stuff over several frames, or wait for stuff, and here is why:

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html