Home c# Unity countdown

Unity countdown

Author

Date

Category

How can I implement the countdown in the game via DateTime? What would we say after 24, some action was performed

Tried it like this, doesn’t count

System.DateTime currentFirstStartupDate = System.DateTime.Today;
    if ((currentFirstStartupDate - System.DateTime.Now) .Seconds == 10f)
    {
      Debug.Log ("is Work");
    }

Answer 1, authority 100%

Here’s an implementation of a timer that just goes down to zero.

// precision to the second
IEnumerator ExecuteAfterTime (float timeInSec)
{
  yield return new WaitForSeconds (timeInSec);
   // do what you want
}

or like this:

// millisecond precision
function Update ()
{
  timeLeft - = Time.deltaTime;
  if (timeLeft & lt; 0)
  {
    // do something at the end of the time
  }
}

If you need to do something every N seconds, then

// millisecond precision
private float nextActionTime = 0.0f;
public float period = 0.1f;
void Update () {
  if (Time.time & gt; nextActionTime) {
  nextActionTime + = period;
  // execute block of code here
  }
}

or like this:

// precision to the second
IEnumerator DoCheck () {
   for (;;) {
     // do something every time seconds
     yield return new WaitForSeconds (time);
   }
}

and run the last block of code:

StartCoroutine ("DoCheck");

Answer 2, authority 25%

Timer:

public float timer;
Void Update () {
 if (timer & gt; 0) timer - = Time.DeltaTime;
 if (timer & lt; 0) timer = 0;
}

Stopwatch:

public float secundomer;
Void Update () {
 if (secundomer & lt; 10) secundomer + = Time.DeltaTime;
 if (secundomer & gt; 10) secundomer = 0;
}

Caroutine can also be used.


Answer 3, authority 25%

I don’t know what you think, but you can try this:

public float Time;
Void Update () {
 Time = Time + Time.deltaTime;
 if (Time = 5) {
  // you code
 }
}

or something like that

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions