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

What's a much better method for a MM/DD/YYY feature?

Discussion in 'Scripting' started by SuperCrow2, Nov 6, 2021.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    The first step is to get the days to go up.

    I couldn't find a single tutorial on how to do that, so I tried to figure it out myself, I feel like I am close, but not only is this not professional and messy, the number glitches when it goes up to the next day, it flickers to get there.

    After 5 seconds it goes to the next day in the month. I had to hard code the next day number after each yield return new because I tried a different method with way less lines of code and the number wouldnt stop going up after it reached the last day of the month.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Day : MonoBehaviour
    7. {
    8.  
    9.  
    10.       public Text dayText;
    11.       public int dayValue = 01;
    12.  
    13.  
    14.  
    15. void Start()
    16.     {
    17.  
    18.         dayText = GetComponent<Text>();
    19.  
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.  
    25.         dayText.text = dayValue + " /";
    26.         StartCoroutine("Wait");
    27.  
    28.     }
    29.  
    30.     private IEnumerator Wait()
    31.     {
    32.      
    33.      
    34.        yield return new WaitForSeconds(5.0f);
    35.         dayValue = 02;
    36.  
    37.         yield return new WaitForSeconds(5.0f);
    38.         dayValue = 03;
    39.  
    40.         yield return new WaitForSeconds(5.0f);
    41.         dayValue = 04;
    42.  
    43.         yield return new WaitForSeconds(5.0f);
    44.         dayValue = 05;
    45.  
    46.         yield return new WaitForSeconds(5.0f);
    47.         dayValue = 06;
    48.  
    49.         yield return new WaitForSeconds(5.0f);
    50.         dayValue = 07;
    51.  
    52.         yield return new WaitForSeconds(5.0f);
    53.         dayValue = 08;
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    Try the C# DateTime type. It has lots of useful methods for dealing with dates, including adding days to a date just like you're trying to do.
     
    Vryken likes this.
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Just FYI, your current script here is going to endlessly start a coroutine every frame.
    You generally shouldn't start a coroutine inside of
    Update
    without some condition to track if it's currently running or not.

    Other than that, use the
    DateType
    type mentioned above.
     
  4. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Thanks.

    That seemed to fix the glitchy flickering of the numbers when the day would increase by 1 once I removed it from Update()