Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Myleek-Chase · Feb 26, 2017 at 01:23 PM · gametimetimer

Timer Countdown fix.

I have a Timer Script that is suppose to count down instead of up but i messed up somewhere. I just need help changing it to be from going up to going down. but I am having a hard time trying to figure out how to switch it. It may be simple but i am missing it. I am still pretty new to C# ;

 public class timerText : MonoBehaviour {
 
     private float count;
     public Text timeText;
     private bool finish = false;
 
     // Use this for initialization
     void Start () {
         //timeText = GetComponent<Text>();
         count = Time.time;
     }
     
     // Update is called once per frame
     void Update () {
         //count -= Time.deltaTime;
         //timeText.text = count.ToString("f0");
         //print(count);
         if (finish) return;
 
         float t = Time.time - count;
         string minutes = ((int)t / 60).ToString();
         string seconds = (t % 60).ToString("f2");
 
         timeText.text = minutes + ":" + seconds;
 
         if (minutes == "1") Done();
     }
 
     public void Done ()
     {
         finish = true;
         timeText.color = Color.red;
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by appslabs · Feb 28, 2017 at 07:33 AM

Timer class

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 
 public class Timer
 {
     /// <summary>
     /// Runs the timer.
     /// </summary>
     /// <returns>The timer.</returns>
     /// <param name="from">From.</param>
     /// <param name="to">To.</param>
     /// <param name="interval">Interval.</param>
     /// <param name="onTimer">On timer.</param>
     public IEnumerator RunTimer(float from, float to, float interval, UnityAction<bool, float> onTimer)
     {
         float dir = Mathf.Sign (to - from);
         float currTime = from;
         onTimer (false, currTime);
         if (dir == 1) 
         {
             while (currTime < to) 
             {
                 yield return new WaitForSeconds (interval);
                 currTime += interval * dir;
                 onTimer (false, currTime);
             }
         }
         else
         {
             while (currTime > to) 
             {
                 yield return new WaitForSeconds (interval);
                 currTime += interval * dir;
                 onTimer (false, currTime);
             }
             
         }
         onTimer (true, currTime);
     }
 }
 

Timer test

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Test : MonoBehaviour 
 {
     public Text txt;
 
     public void Start()
     {
         Timer timer = new Timer();
         StartCoroutine (timer.RunTimer(0, 20, 1, OnTimer));
     }
 
     /// <summary>
     /// Raises the timer event.
     /// </summary>
     /// <param name="isCompleted">If set to <c>true</c> is completed.</param>
     /// <param name="currentValue">Current value.</param>
     public void OnTimer(bool isCompleted, float currentValue)
     {
         if (isCompleted)
         {
             Debug.Log ("Timer completed");
         }
         txt.text = currentValue.ToString();
     }
 }




Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by NecrosDk · Feb 26, 2017 at 05:34 PM

What do you want to count down from?

Easiest way would be to set a float to the time you want to count from, i.e. float timer = 30; - would give you a 30 second timer.

and then in update subtract Time.deltaTime from timer:

timer -= Time.deltaTime;

Time.time returns time passed since level (or is it application?) load, so if you continually use Time.time from start of the scene load, and subtract current Time.time, like you are doing now, you get the time passed since level load, which can only go up.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set the best time and save it? 1 Answer

How to make a timer read to the .001 of a second 2 Answers

Best time and saving best time 3 Answers

Waves go too high after certain time 1 Answer

How do I make a Countdown Timer in Minutes and Seconds only 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges