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. Dismiss Notice

Bug 2D - While loop crashing game

Discussion in 'Scripting' started by tiagosparkz, Sep 13, 2023.

  1. tiagosparkz

    tiagosparkz

    Joined:
    Jul 1, 2022
    Posts:
    7
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class CoalCountdown : MonoBehaviour
    8. {
    9.     public int maximum;
    10.     public float current;
    11.     public Image bar;
    12.     public int delayAmount;
    13.     public float Timer;
    14.  
    15.     void Start()
    16.     {
    17.         Timer = 0;
    18.         delayAmount = 1;
    19.     }
    20.  
    21.  
    22.     void Update()
    23.     {
    24.         Timer = Time.deltaTime;
    25.         getCurrentFill();
    26.     }
    27.  
    28.     void getCurrentFill()
    29.     {
    30.         float fillAmount = current / (float)maximum;
    31.         bar.fillAmount = fillAmount;
    32.     }
    33.     public void increaseBar()
    34.     {
    35.         Timer = 0f;
    36.         while (Timer <= 1)
    37.         {
    38.             current = Timer;
    39.             Debug.Log("Wack");  // crashes the F***ing game
    40.         }
    41.         current = 0f;
    42.     }
    43.  
    44. }
    Another script calls "increaseBar" VIA a button (which only runs this once), when the button is pressed ingame, it freezes right away, I originally thought it was just crashing my whole pc by using 100% gpu/cpu but its just Unity that crashes while my GPU/CPU are as stable as before, need to get up task manager to force close it.

    For reference all I'm trying to do is fill a bar along with the timer, the bars max value is 1 and the timer only goes up to 1 using "time.deltaTime" to update itself in the "void Update" section. So i thought making current = timer, it would go up along with, maybe this works? Can't tell since the game crashes, any help is appreciated.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Here's an exercise for you:
    Code (CSharp):
    1. Timer = 0f;
    2.         while (Timer <= 1)
    3.         {
    4.             current = Timer;
    5.             Debug.Log("Wack");  // crashes the F***ing game
    6.         }
    Go over that loop and consider how the value of Timer changes every iteration the loop executes.
    Hint: no other method is executing while the while loop is executing.
     
    tiagosparkz likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Once your code enters your while loop, it never leaves until Timer > 1. But nothing in your while loop increases Timer.
     
    Bunny83 and tiagosparkz like this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    And being "2D" isn't relevant. :)
     
    Bunny83 likes this.
  5. tiagosparkz

    tiagosparkz

    Joined:
    Jul 1, 2022
    Posts:
    7

    Code (CSharp):
    1. public void increaseBar()
    2.     {
    3.         Timer = Time.deltaTime;
    4.         current = Timer + delayAmount;
    5.  
    6.         while (Timer <= current)
    7.         {
    8.  
    9.             Timer = Time.deltaTime;
    10.             Debug.Log("Wack");  // crashes the F***ing game
    11.  
    12.         }
    13.     }
    Had no clue that Update() wouldn't run if the script is in a while loop thank you.
    I've tried redoing it and came to this above, so the timer is set to the current time, then the delay is added to timer and given to current. Which is then updated while it's in the loop.
    So:
    Timer = 1
    Current = 2

    And it goes up to eventually catch up with current and should stop right? Still crashes the game unfortunately.

    Would it be better to use just Time.Time rather than deltaTime as well?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

    Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

    Absolutely NOTHING will happen... until your code either:

    - returns from whatever function it is running

    - yields from whatever coroutine it is running

    As long as your code is looping, Unity isn't going to do even a single frame of change. Nothing.

    No exceptions.

    "Yield early, yield often, yield like your game depends on it... it does!" - Kurt Dekker
     
    tiagosparkz likes this.
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    I think you're missing a key piece of info.
    Timer = Time.deltaTime;

    This line here. What is the value of Time.deltaTime. Will it ever be > the value of current?

    What you are probably wanting is to add the value to Timer and not just set it equal to it. But, that does bring up another problem. Once you enter the loop, it will run until it's while condition is no longer met. Which means if you expect to do something inside the while loop like update GUI, you'll never see that until the loop finishes. Plus, while in the loop, it will appear frozen.

    Which means you would need something like a coroutine where you can yield within the loop to allow other code to run.