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 /
  • Help Room /
avatar image
0
Question by mortyc · Feb 02, 2019 at 06:03 PM · timer countdowntimer-script

Trying to create a timer that runs outside the Game

Hi all, I am trying to create a timer that runs outside the game. Meaning that even when the application is closed the timer will recharge , in this case "fuel". All it does now is to recharge but once(not until the fuel is maximum) . If you could help me that will be awesome ! Cheers! public ulong lastchestopen; public float msToWait; public bool StartReloadFuel; public string r; public bool Test;

     public int maxFuel;
     public int fuel;
     public int cur_fuel;
 
     private void Awake()
     {
 
         DontDestroyOnLoad(this);
 
         if (FindObjectsOfType(GetType()).Length > 1)
         {
             Destroy(gameObject);
         }
     }
 
     private void Start()
     {
 
         lastchestopen = ulong.Parse(PlayerPrefs.GetString("LastChestOpe"));
         fuel = PlayerPrefs.GetInt("Fuel");
 
         if (!IsChestReady())
         {
             StartReloadFuel = false;
             Test = false ;
         }
         
         
 
     }
     private void Update()
     {
         fuel = Mathf.Clamp(fuel, 0, maxFuel);
         if (fuel < maxFuel && Test )
         {
            Invoke("ChestClick" ,1);
             Test = false;
             
 
          
         }
      
         PlayerPrefs.SetInt("MaxFuel", maxFuel);
         PlayerPrefs.SetInt("Fuel", fuel);
 
         if (fuel >= maxFuel)
             {
             StartReloadFuel = false;
             Test = false;
             }
 
         
 
 
         if (!StartReloadFuel)
         {
             if (IsChestReady())
             {
                 StartReloadFuel = true;
                 return;
             }
             //set timer
             ulong diff = ((ulong)DateTime.Now.Ticks - lastchestopen);
             ulong m = diff / TimeSpan.TicksPerMillisecond;
             float secondsLeft = (float)(msToWait - m) / 1000.0f;
             r = "";
             secondsLeft -= ((int)secondsLeft / 3600) * 3600;
             //minutes
             r += ((int)secondsLeft / 60).ToString("00") + "m ";
             //seconds
             r += ((int)secondsLeft % 60).ToString("00") + "s "; ;
             //chestTimer.text = r;
 
             
         }
         PlayerPrefs.SetString("r", r);
         
     }
   
     IEnumerator TestingFuel()
     {
         while (fuel < maxFuel)
         {
   
             yield return new WaitForSeconds(1);
         }
     }
     public void ChestClick()
     {
         lastchestopen = (ulong)DateTime.Now.Ticks;
         PlayerPrefs.SetString("LastChestOpe", DateTime.Now.Ticks.ToString());
         PlayerPrefs.SetInt("Fuel", fuel);
         StartReloadFuel = false;
 
     }
 
     private bool IsChestReady()
     {
         ulong diff = ((ulong)DateTime.Now.Ticks - lastchestopen);
         ulong m = diff / TimeSpan.TicksPerMillisecond;
         float secondsLeft = (float)(msToWait - m) / 1000.0f;
         if (secondsLeft < 0)
         {
 
             if (fuel < maxFuel)
             {
                 Test = true;
                 AddFuel(2);
                 fuel = cur_fuel;
             }
             return true;
         }
 
         return false;
     }
 
     public void AddFuel(int newFuel)
     {
         cur_fuel = fuel + newFuel;
 
 
     }
     public void SubFuel(int newFuel)
     {
         fuel = cur_fuel - newFuel;
 
 
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by tormentoarmagedoom · Feb 02, 2019 at 08:36 PM

Good day.

I will not read your code.

If you pretend something that "works" while is not running, the only thing you can take as a reference is the actual real time. This means register Year, month, dai, hour and minute of the moment it commence to "count".

Then compare to the actual Year, month, dai, hour and minute, so oyu know how many minutes passed from the time it was commenced, so you can calculate how many fuel it have now.

System.DateTime.Now.Hour

Bye.

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

163 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 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

How can i make countdown tick-tock sound ? 0 Answers

Farming game Time management 0 Answers

How to get a countdown timer with values entered by the player 0 Answers

How do I stop a scene from switching from switch before the Countdown timer reaches zero 2 Answers

Creating a timer that starts when my 3rd scene is loaded 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