For script crashes unity

What’s wrong with my for script. My intent with this script is to dash in midair and have some stall time which is why I have _gravity = 0;

 else if (DashMoves == 1 && Input.GetKeyDown("d") && Grounded == false)
        {
            _moveSpeed = 20;
            DashMoves--;
            Debug.Log("Dash Right");
            for (float i = 0; i <= 1; i = 0 + Time.deltaTime)
            {
                _gravity = 0;
            }

P.S. I do set gravity back to its original number and I’m a still very new to coding so I know my script is a little sloppy.

0 + Time.deltaTime will always be less or equal to 1. This will make this loop run infinitely, so unity crash

1 Like

So your “for” loop continues while i <= 1. But i is always equal to Time.deltaTime. For Time.deltaTime to be greater than 1, you’d need this for loop to just happen to start after a frame which took over a full second to render. Otherwise you get stuck into an infinite loop.

You should probably change i = 0 + Time.deltaTime to i += Time.deltaTime.

1 Like

Should I make it i = i + Time.deltaTime;

Oh yeah I should. Thanks

Loops are not the place to do delays. They will try to run as fast as possible. Time.deltaTime won’t change in this case as it’s all in the same frame (unless I’m missing something). Regardless, it won’t matter, because it will loop extremely fast and then exit.

Use coroutines or Invoke() for delays.

1 Like