Unity crashes when a while starts

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.

void Update()
    {
        if(Input.GetKeyDown(KeyCode.W))
        {
            if (canMoveW == true)
            {
                Pos = transform.position;
                Instantiate(pTail, new Vector3(Pos.x, Pos.y, Pos.z), Quaternion.identity);
                while(transform.position.y != Pos.y + 1)
                {
                    transform.Translate(0, moveSpeed, 0);
                }
                StartCoroutine(WaitForMove(WaitSpeed));
            }
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            if (canMoveS == true)
            {
                Pos = transform.position;
                Instantiate(pTail, new Vector3(Pos.x, Pos.y, Pos.z), Quaternion.identity);
                while (transform.position.y != Pos.y + 1)
                {
                    transform.Translate(0, -moveSpeed, 0);
                }
                StartCoroutine(WaitForMove(WaitSpeed));
            }
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            if (canMoveA == true)
            {
                Pos = transform.position;
                Instantiate(pTail, new Vector3(Pos.x, Pos.y, Pos.z), Quaternion.identity);
                while (transform.position.y != Pos.y + 1)
                {
                    transform.Translate(-moveSpeed, 0, 0);
                }
                StartCoroutine(WaitForMove(WaitSpeed));
            }
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            if (canMoveD == true)
            {
                Pos = transform.position;
                Instantiate(pTail, new Vector3(Pos.x, Pos.y, Pos.z), Quaternion.identity);
                while (transform.position.y != Pos.y + 1)
                {
                    transform.Translate(moveSpeed, 0, 0);
                }
                StartCoroutine(WaitForMove(WaitSpeed));
            }
        }
    }

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”.

5 Likes

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: