• 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
Question by TakMarche · Nov 10, 2013 at 07:47 PM · timedatetimedifferenceminutes

Check How Many Minutes Have Passed

I have the following script below set the currentDate to the current time when the user launches the app and sets the time they had when clocking it to the oldTime. Currently I'm trying to figure out how many minutes have passed Ex: 1:00PM - 2:00PM is a total of 60 minutes. I'm trying to figure out how the game Candy Crush worked with it's life timer where you get 1+ life after every half hour, basically I'm trying to produce the same effect except with every 5 minutes. Looked around for a good amount of time and I didn't find much that would help me.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class LifeTimer : MonoBehaviour {
 
     DateTime currentDate;
     DateTime oldDate;
 
     double minutes;
     void Start () 
     {
         currentDate = System.DateTime.Now;
         long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
         DateTime oldDate = DateTime.FromBinary(temp);
         print("oldDate: " + oldDate);
         TimeSpan difference = currentDate.Subtract(oldDate);
         print("Difference: " + difference);
 
     }
 
     void OnApplicationQuit()
     {
         PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
         print("Saving this date to prefs: " + System.DateTime.Now);
     }
 
     void Update()
     {
         
     }
 
     void OnGUI()
     {
         long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
         GUI.Label(new Rect(10, 10, 300, 30), "Last Time Logged: " + DateTime.FromBinary(temp));
         GUI.Label(new Rect(10, 40, 300, 30), "Current Time: " + System.DateTime.Now);
     }
 }
Comment
flckev

People who like this

1 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Cains · Nov 10, 2013 at 08:52 PM

I've coded the below example which will calculate the difference in minutes from the time the application is started to the current time, then every frame it checks if a life should be given against the time and how many lives have already been given. I suspect in your case you'd want to set oldDate, lives, and livesGiven to a PlayerPref containing these values from when they first started playing.

Note that player prefs are not secure and a skilled player could go in and edit his time, lives, or livesGiven.

 using UnityEngine;
 using System.Collections;
 using System;
  
 public class LifeTimer : MonoBehaviour {
  
     DateTime oldDate;
     DateTime currentDate;
 
     int minutes;

     int livesGiven;
     int lives;
 
     void Start () 
     {
         lives = 0;
         minutes = 0;
         oldDate = System.DateTime.Now;
  
     }
  
     void Update()
     {
         currentDate = System.DateTime.Now;
         minutes = currentDate.Minute - oldDate.Minute;

         if(minutes/5 > livesGiven) {
             lives++;
             livesGiven++;
         }
     }
 
 }
Comment
flckev
MrWy07

People who like this

2 Show 1 · 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 gothamsknight217 · Nov 17, 2017 at 05:04 PM 0
Share

If the hour is changed I dont think this is going to work.

For example; if the app is opened at 10:51am, when Start() is called your oldDate.Minute will be 51. currentDate.minute will be the updated minute every frame so 52, 53, 54, 55, 56... so on.

Once it becomes 11:00am, your currentDate.minute becomes 0. So now your calculation "minutes = currentDate.Minute - oldDate.Minute;" returns 0 - 51 = -51. Its going to return a negative int which will never be > livesGiven and thus a life wont be given. .

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Save current time on quit, then on relaunch compare to new current time and get difference? 3 Answers

C# Check If Minutes equal some denomination of 5 3 Answers

How to make notification appear at certain time without having to press button again? 2 Answers

Percentage between DateTime 1 Answer

How to compare two dates? 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