Home > Blockchain >  Unable to break for loop in unity which causes it to freeze
Unable to break for loop in unity which causes it to freeze

Time:06-11


 public void FixedUpdate()
       {
        
        for (int direction = 1; direction < 5;direction   )
        {
            if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
            {
                direction = 1;
                continue;
            }
            else if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
            {
                direction = 2;
                continue;
            }
            else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
            {
                direction = 3;
                continue;
            }
            else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
            {
                direction = 4;
                continue;
            }
            if (direction == 1)
            {
                RB.velocity = transform.up * speed;
                continue;

            }
            else if (direction == 2)
            {
                RB.velocity = -transform.up * speed;
                continue;
            }
            else if (direction == 3)
            {
                RB.velocity = -transform.right * speed;
                continue;
            }
            else if (direction == 4)
            {
                RB.velocity = transform.right * speed;
                continue;
            }
            break;

This the code I worte for changing the direction of bullet in for loop and trying to break it but it doesn't break at all I know that I have to attach the script to unity editor but i don't know how to break the loop while debuging

CodePudding user response:

What is it that your code is meant to do? To be frank you should rewrite it, but you can solve this problem in particular by removing all the "continue" and "break" statements. What's happening is (if you are pressing Up for example) your loop is running with direction == 1, your code detects it and continues the loop with direction == 2. Then you detect Up again and set direction to 1. This repeats forever.

But really, I think you want to check for each arrow independently. That needs no loop and no else. Just write the code to check each arrow key.

CodePudding user response:

Pay attention to the variable you are you using for the loop. Don't assign any value to it because you will risk having an endless loop.

for (int direction; direction < 5; direction  ){
   direction = 1;
}

This loop will never stop.
Also, continue; statement will ensure that the rest of the code block will not execute hence you will never reach break; at the end there, in other words, once continue; is reached it will skip to the next iteration.
I suggest you remove the loop and assign your direction variable within the update() method. Take a look at this : https://answers.unity.com/questions/409507/eliminating-input-loss.html

  • Related