• 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 /
  • Help Room /
avatar image
0
Question by Roromihn · Feb 03, 2018 at 12:59 AM · timer countdowntimespansubstract

Set a Timer countdown from a TimeSpan

Hi! I've been dealing with this for two days and I don't know what else to try so, hoping someone can help. I have set a timer which shows the remaining hours until the player can access the Level again by comparing the time difference between GameOverTime and StartTime (both saved on PlayerPrefs; which I know it's not the safest option but it has to be like this for now). My problem is that I want that timer to keep counting down while it's being displayed on screen. And I don't know how to do that. Basically, if the remaining time is 5:15:08 I want it to keep going down from there to 5:15:07, 5:15:06, etc. I've tried using several things I've found online but none has worked so far :/

The latest thing I came up with was to provide the player with a button to update the time (kind of a "refresh page" thingy) but the way I have it, it doesn't work either...

Here is my code:

private bool unlocked; public string level;

 public GameObject levelLocked;
 public GameObject holder;
 public GameObject holder2;
 public GameObject holder3;


 public GameObject timeLeftObject;
 public DateTime startTime;
 public DateTime GameOverTime;
 public TimeSpan timeDifference;
 public Text timer;
 private String timeRemaining;
 private TimeSpan timeDifference2;
 public TimeSpan timeToPass = new TimeSpan (8,0,0);
 public TimeSpan timeToRestart1;


 // Use this for initialization
 void Start () {

     PlayerPrefs.SetInt ("Level1", 1);
     startTime = DateTime.Now;

     GameOverTime = Convert.ToDateTime(PlayerPrefs.GetString ("LastTimeGameOver"));
     Debug.Log(GameOverTime);

     timeDifference = startTime - GameOverTime;
     timeToRestart1 = timeDifference.Subtract (timeToPass);


     Debug.Log (timeDifference);

     timer = GetComponent<Text> ();
     timeRemaining = string.Format ("{0:D2}:{1:D2}:{2:D2}", Math.Abs(timeToRestart1.Hours), Math.Abs(timeToRestart1.Minutes), Math.Abs(timeToRestart1.Seconds));

     Debug.Log ("Ha cambiado");


     if (PlayerPrefs.GetInt ("Level21") == 1) {

         holder.SetActive (false);
         holder2.SetActive (true);
     }

     if (PlayerPrefs.GetInt ("Level41") == 1) {

         holder.SetActive (false);
         holder2.SetActive (false);
         holder3.SetActive (true);

     } else {
         
         holder.SetActive (true);
         holder2.SetActive (false);
         holder3.SetActive (false);

 } 

}

 // Update is called once per frame
 void Update () {
     


     if (timeDifference.Hours < 8) {

         timeLeftObject.SetActive (true);
         timer.text = timeRemaining;

         Debug.Log ("Menor que 8 horas");
         
     } if(timeDifference.Hours >= 8){

         timeLeftObject.SetActive (false);
         Debug.Log ("Más o igual a 8 horas");
     }
 
 }
         

 public void SelectLevel (string level)
 {
         
     if (PlayerPrefs.GetInt(level) == 1){

         unlocked = true;
         SceneManager.LoadScene (level);

     } else {

         unlocked = false;
         levelLocked.SetActive (true);

         if (Input.GetKeyDown(KeyCode.Space)) {

             levelLocked.SetActive (false);
             unlocked = false;
         }
     }
 }

 public void UpdateTime(){

     if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown(0)) {

         timer.text = timeRemaining;
     }
 }

 }
     

I'm a noob in programming so maybe the answer is really simple... Any help is very much appreciated! Thanks!

Comment
Add comment · Show 1
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 Vollmondum · Apr 22, 2018 at 06:15 PM 0
Share

Spent 12 hours with that TimeSpan today. Sum up: forget it and get to simple float math ins$$anonymous$$d :)

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

124 People are following this question.

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

Related Questions

Formatting a timespan into a relative date in JavaScript 0 Answers

Player Respawn 0 Answers

Add a Delay at the start of a level 1 Answer

Problems showing accurate time with FixedUpdate() 0 Answers

How do I create a timer. 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