0

I am facing an issue with my timer. The issue is, timer goes negative after reaching the specified time limit and the next levels never unlocks. This only happens whenever I change my timespan to 24 hours. When I am using seconds or minutes the timer stops when it reaches to zero (0) and make the button interactable again. I have also tested it on 1 hour and it works fine.

IEnumerator TimeUpdate()
{
    if (bonus != null)
    {
            if (PlayerPrefs.GetInt("Bonus", 0) == 0)
            {
                bonus.interactable = true;
                showTimer.gameObject.SetActive(false);
                PlayerPrefs.SetString("BONUS_END_TIME", "");

                StopAllCoroutines();
            }
            else
            {
                bonus.interactable = false;
                showTimer.gameObject.SetActive(true);
            }
        
    }

    while (true)
    {
        chkbonustime();

        DateTime dt = DateTime.Now;

        string bonusendtime = PlayerPrefs.GetString("BONUS_END_TIME", "");
        DateTime dateComplete;

        if (bonusendtime != null)
        {
            dateComplete = DateTime.Parse(bonusendtime);
            DateTime ENDTIME = dateComplete.Add(TimeSpan.FromHours(24));

            TimeSpan ABC = ENDTIME - dt;

            showTimer.text = ABC.Hours + " : " + ABC.Minutes + " : " + ABC.Seconds;
        }
         
        // Debug.Log();

        yield return new WaitForSeconds(1);
    }
}

public void chkbonustime()
{

    string bonusendtime = PlayerPrefs.GetString("BONUS_END_TIME", "");

    if (!bonusendtime.Equals(""))
    {
        DateTime dateComplete = DateTime.Parse(bonusendtime);
        DateTime xyz = DateTime.Now;

        TimeSpan timespan = xyz - dateComplete;
        Debug.Log(timespan.Seconds);
        if (timespan.Hours >= 24)
        {
            // if (PlayerPrefs.GetInt("Bonus", 0) == 1)
            // {
            PlayerPrefs.SetInt("Bonus", 0);
            bonus.interactable = true;
            showTimer.gameObject.SetActive(false);
            PlayerPrefs.SetString("BONUS_END_TIME", "");

            StopAllCoroutines();
            //  }
            //  else
            // {
            //     bonus.interactable = false;
            //     showTimer.gameObject.SetActive(true);
            // }
        }
        //else
        //  return false;
        // PlayerPrefs.SetString("BONUS_END_TIME", "");
    }
    else
    {

        // return false;
    }
}
vzwick
  • 11,008
  • 5
  • 43
  • 63

1 Answers1

2

Hours is only between 0-23, you likely want TotalHours or TotalDays.

Martheen
  • 5,198
  • 4
  • 32
  • 55
  • Got it. So my code would look something like this, 'code' if (timespan.Totalhours>=24) { //do something } but is this line of code OK or do I have to change anything here as well? 'code' DateTime ENDTIME = dateComplete.Add(TimeSpan.FromHours(24)); – Muhammad Rehmat Dec 20 '21 at 11:48
  • FromDays(1) seems easier to understand if you're dealing with days, but yeah FromHours(24) works too... unless your timezone deal with DST https://stackoverflow.com/questions/47160663/datetime-tolocaltime-not-working-properly-with-daylight-savings – Martheen Dec 20 '21 at 12:05