Crashing during a do...while loop.

Hello, I’m not extremely new to the software development scene and I’ve recently got back into coding for games. I’m running into an issue, where the code does indeed work but Unity is crashing when the player moves a certain distance.

If I created a memory leak, I wouldn’t know how.

Let me go ahead and explain what it is I’m trying to do as well what I did before.

I want the player(character) to move a certain distance after x amount of distanceMoved.
It does indeed work if not included in any loop.

Here is the loop I’m working with, I’ve tried in the void Update() and void FixedUpdate and neither are helping. I swapped the variable distanceMoved from a float to an int hoping it would’ve solved the issue and it did not.

The past few times I’ve crashed way before even reached the threshold for the speed increase, but enough of my yammering, if anyone could lend a hand that’d be great. Here is the code fragment :

        // Speed Increase Loop
        if (distanceMoved >= 100 && maxPlayerSpeed <= 6.5f)
        {
            speedIncrease = true;

            do
            {
                playerSpeed += 0.030f;
            } while (speedIncrease == true && maxPlayerSpeed <= 6.5f);

        }
        else
        {
            speedIncrease = false;
        }

You’re causing an unbreakable loop.

You’re increasing playerSpeed += 0.3f, BUT, Unity isn’t jumping out of that specific do loop to actually update anything else (ex: speedIncrease to false, or maxPlayerSpeed <= 6.5f.

How would I fix that exactly?

Try putting your do loop in a Coroutine and add a yield of X seconds.

you can even add: while (playerSpeed <= maxPlayerSpeed) (assuming maxPlayerSpeed is 6.5f).

1 Like

What is your exit condition?

But right now, your loop never exits. Does speedincrease become false or maxPlayerSpeed ever > 6.5f?

Honestly though, this may be better in a coroutine. Otherwise, it could tie up your game while it’s in the loop (thus why you freeze) Since you enter a loop you never exit.

I actually figured it out, works quite nicely. Thanks for the help :slight_smile:

For anyone that has an issue like this in the future here is the example I came up with.

        while (distanceMoved >= 100 && playerSpeed <= maxPlayerSpeed)
        {
            speedIncrease = true;

            if (speedIncrease == true)
            {
                playerSpeed += 0.035f;
            }
            else
            {
                speedIncrease = false;
            }

            return;
        }

:eyes:

2 Likes

If you want it to increase gradually, you should use a coroutine… which would give at least some time for it to happen.
The “working” solution you have doesn’t make a lot of sense, because the loop will run during the same frame, which even if that doesn’t cause a problem with your game, would be essentially the same as setting playerSpeed to maxPlayerSpeed (without the loop). :slight_smile:
Plus, setting speedIncrease to true then checking if it’s true or false isn’t making sense, but that’s a different issue, I think.

That’s more complex then you need. This single line does the same thing.

if (distanceMoved >= 100) playerSpeed = maxPlayerSpeed;