Muhammad Rehmat
Muhammad Rehmat

Reputation: 1

Timer Goes Negative

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;
    }
}

Upvotes: 0

Views: 108

Answers (1)

Martheen
Martheen

Reputation: 5580

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

Upvotes: 2

Related Questions