Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Crashing during a do...while loop.

Discussion in 'Scripting' started by UDN_861aa904-6b1a-4cdd-a2f6-3cbcc5cb83a6, Jun 14, 2017.

  1. UDN_861aa904-6b1a-4cdd-a2f6-3cbcc5cb83a6

    UDN_861aa904-6b1a-4cdd-a2f6-3cbcc5cb83a6

    Joined:
    Apr 29, 2016
    Posts:
    3
    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 :

    Code (csharp):
    1.  
    2.         // Speed Increase Loop
    3.         if (distanceMoved >= 100 && maxPlayerSpeed <= 6.5f)
    4.         {
    5.             speedIncrease = true;
    6.  
    7.             do
    8.             {
    9.                 playerSpeed += 0.030f;
    10.             } while (speedIncrease == true && maxPlayerSpeed <= 6.5f);
    11.  
    12.         }
    13.         else
    14.         {
    15.             speedIncrease = false;
    16.         }
    17.  
     
  2. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    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.
     
  3. UDN_861aa904-6b1a-4cdd-a2f6-3cbcc5cb83a6

    UDN_861aa904-6b1a-4cdd-a2f6-3cbcc5cb83a6

    Joined:
    Apr 29, 2016
    Posts:
    3
    How would I fix that exactly?
     
  4. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    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).
     
    YektaOz likes this.
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    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.
     
  6. UDN_861aa904-6b1a-4cdd-a2f6-3cbcc5cb83a6

    UDN_861aa904-6b1a-4cdd-a2f6-3cbcc5cb83a6

    Joined:
    Apr 29, 2016
    Posts:
    3
    I actually figured it out, works quite nicely. Thanks for the help :)

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

    Code (csharp):
    1.  
    2.         while (distanceMoved >= 100 && playerSpeed <= maxPlayerSpeed)
    3.         {
    4.             speedIncrease = true;
    5.  
    6.             if (speedIncrease == true)
    7.             {
    8.                 playerSpeed += 0.035f;
    9.             }
    10.             else
    11.             {
    12.                 speedIncrease = false;
    13.             }
    14.  
    15.             return;
    16.         }
    17.  
     
  7. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    o_O
     
    samizzo and BlackPete like this.
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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). :)
    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.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's more complex then you need. This single line does the same thing.

    Code (CSharp):
    1. if (distanceMoved >= 100) playerSpeed = maxPlayerSpeed;