Search Unity

Problem with Timer

Discussion in 'Scripting' started by psychonotic, May 15, 2020.

  1. psychonotic

    psychonotic

    Joined:
    Mar 28, 2020
    Posts:
    3
    Hi all,

    i am trying to set up a 20 minute count down timer but i am having trouble resetting the timer back to 20, right now it is set up to count down to 19 then add 1 minute when completed but it is returning "Resetting" in the console every cycle and not applying the changes to TwentyMinTimer. What am i missing here?

    Thanks in advance for your help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Timer : MonoBehaviour
    7. {
    8.     private float startTime;
    9.     public string countDownTimer;
    10.     private int TwentyMinTimer;
    11.     private int SecondsCounterDown;
    12.  
    13.     void Start()
    14.     {
    15.         DontDestroyOnLoad(this.gameObject);
    16.         startTime = Time.time;
    17.         InvokeRepeating("TimeSinceStart", 0.01f, 1.0f);
    18.  
    19.     }
    20.  
    21.     public void TimeSinceStart()
    22.     {
    23.         float t = Time.time - startTime;
    24.  
    25.         int minutesInt = (int)t / 60;
    26.         int SecondsInt = (int)t % 60;
    27.  
    28.         TwentyMinTimer = 20 - minutesInt;
    29.         SecondsCounterDown = 59 - SecondsInt;
    30.  
    31.         print(TwentyMinTimer + ":" + SecondsCounterDown);
    32.  
    33.         if (TwentyMinTimer > 19)
    34.         {
    35.             TwentyMinTimer = TwentyMinTimer + 1;
    36.             print("Resetting");
    37.             //add life
    38.         }
    39.  
    40.     }
    41.  
    42. }
     
  2. psychonotic

    psychonotic

    Joined:
    Mar 28, 2020
    Posts:
    3
    sorry some parts of the code is named poorly, work in progress.
     
  3. psychonotic

    psychonotic

    Joined:
    Mar 28, 2020
    Posts:
    3
    i manager to work it out, realized my < was not a > and also changed
    Code (CSharp):
    1. if (TwentyMinTimer > 19)
    2.         {
    3.             TwentyMinTimer = TwentyMinTimer + 1;
    4.             print("Resetting");
    5.             //add life
    6.         }
    for


    Code (CSharp):
    1.         if (TwentyMinTimer < 19)
    2.         {
    3.             startTime = Time.time;
    4.         }
    5.  
     
    Kurt-Dekker likes this.
  4. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Rather than Time.time - timeStart you can also increment Time.timeDelta.

    Code (CSharp):
    1. float timeSpend += Time.timeDelta
    Also rather than to that minute / second calculation in your update you do that on start :

    Code (CSharp):
    1. int timeEnd = 60 * 20; // for a 20 min game
    And then from your update :

    Code (CSharp):
    1. void LateUpdate()
    2. {
    3.        timeSpent += Time.deltaTime;
    4.        if (timeSpend >= timeEnd)
    5.        {
    6.             // game over
    7.        }
    8. }