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

While in Update or multiple coroutins? Need advice.

Discussion in 'Scripting' started by zurisar, Feb 12, 2020.

  1. zurisar

    zurisar

    Joined:
    Nov 4, 2019
    Posts:
    24
    Hello guys! I countinue learn unity and need advice how to make countdown timers.

    In my project player can produce some items, I want to make countdown for produce.

    I think about two ways how to do it.

    1st use while loop in Update function, just get System.DateTime and if item on produce block item slot and while datetime now > datetime start + produce time.

    2nd way use coroutines, but I need some qty of coroutines. Need to assign some id to coroutine and then add items when it stop.

    What way better? And why?
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,004
    Don’t use coroutines, but I’m not sure why you would use a while in update?
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Don't use a while Loop in Update, simply count down using Time.DeltaTime until you reach Zero.

    A coroutine is also an Option, but it is slightly more involved, and threfore more difficult to debug if you are new to Unity.

    Once you get more comfortable with Unity, you might consider writing a dedicated Timer class that you can use to Trigger Actions/Events on your objects.
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,921
    Coroutines are great for fire&forget stuff. "yield for 2 seconds then do X" is very easy in a coroutine. If you want to move a bar as it goes, it's a little more work (while(not done yet) { move lifebar; yield return null; }), but still easier.

    But coroutines are a pain to play with while they run. For example, you want to pause the timer, or switch what it does, or a power-up can make it go X2 speed part-way through, or someone else wants to watch the time left. Either way, that stuff is a pain, but less of a pain if Update handles it.

    But changing isn't all that hard. It seems fine to use an easy coroutine at first, see how things go, then change it up if the coroutine becomes too difficult to work with.
     
  5. zurisar

    zurisar

    Joined:
    Nov 4, 2019
    Posts:
    24
    Thanks for advices guys! I want to try with coroutins, but I don't understand something, I try to delay supply delivery time, I use that code:
    Code (CSharp):
    1.  public void Buy()
    2.     {
    3.         /*
    4.         int amount = int.Parse(input.text);
    5.         Debug.Log("sCP " + item + " " + amount);
    6.         Inventory.instance.BuyItem(item, amount);
    7.         */
    8.         StartCoroutine(Order());
    9.         ClosePanel();
    10.     }
    11.  
    12.     IEnumerator Order()
    13.     {
    14.         Currency currency = Factory.instance.GetComponent<Currency>();
    15.         int amount = int.Parse(input.text);
    16.         int pricetotal = item.priceBuy * amount;
    17.         Debug.Log("start coroutine");
    18.         if (currency.ChargeFromBalance(pricetotal))
    19.         {
    20.             Debug.Log("StartTimer");
    21.             yield return new WaitForSeconds(5);
    22.             Debug.Log("End timer");
    23.             Inventory.instance.BuyItem(item, amount);
    24.         }
    25.         else
    26.         {
    27.             yield return null;
    28.             GameManager.instance.Message("", "");
    29.         }
    30.     }
    In log I see this

    start coroutine
    StartTimer


    And that's all, what I do wrong?
     
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,921
    Coroutines have been around for a long time. You should be able to find lots of examples and tips with a general Search. In this case, which you can read more about, it could be how they're attached to the gameObject that started them. If you Destroy something with a coroutine on it, the coroutine dies with it.