• 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 lokvisnu · Mar 14, 2018 at 03:45 AM · timetimer-scriptdatetime

I need a 2 hour realtime timer for my game

 private int Gems;
     public GameObject Main;
     private int Coins;
     public GameObject NoEnough;
     private string B;
     public GameObject BuyManagerObject;
     public Text TimeText;
     public float mstoWait;
     public GameObject Bo;
     private string Booster;
     private ulong lastbooster;
     private void Start()
     {
         //lastbooster = ulong.Parse(PlayerPrefs.GetString("LastBooster"));
         Booster = PlayerPrefs.GetString("BoosterAct");
         lastbooster = ulong.Parse (PlayerPrefs.GetString("LastBooster"));
         if (!IsChestReady())
         {
             Main.SetActive(true);
             PlayerPrefs.SetString("BoosterAct", "true");
            
         }
 
     }
    private bool IsChestReady()
     {
         ulong diff = ((ulong)DateTime.Now.Ticks - lastbooster);
         ulong m = diff / TimeSpan.TicksPerMillisecond;
         float SecondsLeft = (float)(mstoWait - m) / 1000.0f;
      if (SecondsLeft < 0)
             return true;
 
         return false;
     }
     public void Gems450(){
         Gems = PlayerPrefs.GetInt("NoofDiamond");
         if(Gems <= 450)
         {
             Gems -= 450;
             PlayerPrefs.SetInt("NoofDiamond", Gems);
             lastbooster = (ulong)DateTime.Now.Ticks;
             PlayerPrefs.SetString("LastBooster",lastbooster.ToString());
             PlayerPrefs.SetString("BoosterAct", "true");
             PlayerPrefs.SetInt("BoosterTimer", 7200);
             PlayerPrefs.SetInt("Type",1);
             Bo.SetActive(false);
             Main.SetActive(true);
             Set();
         }
         else
         {
             NoEnough.SetActive(true);
         }
 }
     public void OK()
     {
         PlayerPrefs.SetInt("N",0);
     }
     private void Update()
     {
         Booster = PlayerPrefs.GetString("BoosterAct");
         if (Booster == "true")
         {   
             
             ulong diff = ((ulong)DateTime.Now.Ticks - lastbooster);
             ulong m = diff / TimeSpan.TicksPerMillisecond;
             float SecondsLeft = (float)(mstoWait - m) / 1000.0f;
             string r = "";
             //hour
             r += ((int)SecondsLeft / 3600).ToString() + "h ";
             SecondsLeft -= ((int)SecondsLeft / 3600) * 3600;
             //minutes
             r += ((int)SecondsLeft / 60).ToString("00") + "m ";
             //Seconds
             r += (SecondsLeft % 60).ToString("00") + "s ";
             TimeText.text = r;
             PlayerPrefs.SetString("BoosterAct","true");
             Booster = PlayerPrefs.GetString("BoosterAct");
             Bo.SetActive(false);
             Main.SetActive(true);
             if (IsChestReady())
             {
                 PlayerPrefs.SetString("BoosterAct", "false");
             }
 
         }
         
         else
         {
             Bo.SetActive(true);
             Main.SetActive(false);
            
         }
     }
     public void Set()
     {
         mstoWait = PlayerPrefs.GetInt("BoosterTimer");
         Debug.Log(mstoWait);
         Main.SetActive(true);
     }

This was the code I used but It Didnt woark properly. The Trouble Is caused while the scene Starts. The variable 'SecondsLeft ' Gets back to zero. Can Some One Help Me please. Thank you

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

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

127 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 avatar image avatar image avatar image

Related Questions

Alarm Clock Using System Time 0 Answers

Seconds from now to seven o'clock. 1 Answer

Farming game Time management 0 Answers

How to make an App that only works for a week? 1 Answer

Looking For A Better Way To Check If Day Has Passed 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