Bug or Error maybe?

Every time I enter this code

while (Input.GetKeyDown(KeyCode.W))
{
   rb.AddRelativeForce(Vector3.up * thrust);
   mainEngineThruster.Play();
}

Unity will freeze up indefinitely and I have to kill the process and restart Unity.
Not sure if this is a bug error or my stupidity :slight_smile: Didn’t know where to post it.

Its an infinite loop. There is nothing inside the loop to cause it to exit. What you want is to execute this across multiple frames. Like this.

void Update ()
{
    if (Input.GetKeyDown(KeyCode.W))
    {
        rb.AddRelativeForce(Vector3.up * thrust);
        mainEngineThruster.Play();
     }
}

Yah I understand but should it not through an error in Unity or warning or anything, not just lock the program up is what Im aiming at.

The problem is that there is no error, the code functioning as it was written. It’s not really locked, it is just infinitely looping. Use while/until with care. Now you know what not to do. :wink:

There is some experiance and skill required in coding. The first lesson you learn is computers will do exactly what you tell them. If you tell them to lock up forever in an endless loop they will. Just be glad there is a fail safe built into your CPU that prevents it from melting.

1 Like