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 JackTenSeven · Aug 16, 2016 at 08:22 PM · c#timer countdown

DateTime Timer not working when loading the correct timer amount??

I have a timer that counts down from 120 and using DateTime to save the date so that if you close the game and come back the timer has been going down based on the system date. Kind of like a when you build something in Clash of Clans and you can come back into the game later and the timer has gone down. (I dont need to have a system clock to prevent Date Time cheating). Here is my script. It is long but thats because it is also for other crafting things.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System;
 public class Craft : MonoBehaviour {
     DateTime currentDate;
     DateTime oldDate;
 
     public float tinpaneltimer;
     public bool tinpaneltimerOn = false;
 
     public string saveLocation;
     public static Craft instance;
 
 
     public Text CraftSteelButtonText;
     public Text CraftElectrumButtonText;
     public Text CraftTinPanelButtonText;
     public int Iron;
     public int Coal;
     public int Steel;
 
     public int Silver;
     public int GoldOre;
     public int Electrum;
 
     public int Diff;
 
     public int Tin;
     public int TinPanel;
 
 
 
     public int XP;
     public int XPCap;
     public int Level;
     public int XPCapMulitplier;
 
     public Text SteelAmount;
     public Text ElectrumAmount;
     public Text TinPanelAmount;
 
     public Text SlotSteelText;
     public Text SlotElectrumText;
     public Text SlotTinPanelText;
 
     public bool CraftSteelOn = false;
     public bool CraftElectrumOn = false;
     public bool CraftTinPanelOn = false;
 
 
     public Button CraftSteelButton;
     public Image CraftSteelButtonImage;
     public Image CraftSteelButtonImageRight;
 
     public Button CraftElectrumButton;
     public Image CraftElectrumButtonImage;
     public Image CraftElectrumButtonImageRight;
 
     public Button CraftTinPanelButton;
     public Image CraftTinPanelButtonImage;
     public Image CraftTinPanelButtonImageRight;
 
     public Image Panel1;
     public Image Panel2;
     public Image Panel3;
 
     public Image LevelBar;
 
     public AudioSource Audio;
 
 
     void Start()
     {
         
 
         CheckDate ();
         if (tinpaneltimer != 120) {
             tinpaneltimerOn = true;
             print ("Tinpaneltimer was not at 120 so it had already started which means that timer should be On");
         }
         if (tinpaneltimer == 0) {
             tinpaneltimer = 120;
             print ("Timer was 0 so it was reset to 120");
         }
 
 
         //if (tinpaneltimerOn == true) {
             
             //tinpaneltimer -= instance.CheckDate ();
         //}
         //not sure what this does, could possibly interfere.
 
             
     }
 
     void Awake()
     {
         
         instance = this;
 
         saveLocation = "lastSavedDate1";
 
     }
 
     public float CheckDate()
     {
         currentDate = System.DateTime.Now;
         string tempString = PlayerPrefs.GetString (saveLocation, "1");
         long templong = Convert.ToInt64 (tempString);
         DateTime oldDate = DateTime.FromBinary (templong);
         print ("Old Date: " + oldDate);
         TimeSpan difference = currentDate.Subtract (oldDate);
         TimeSpan dt = TimeSpan.FromSeconds(120) - difference;
         print ("Timer should be " + dt);
          //Figure out how to subtract int and timespan// //Did that but maybe be something else setting it to 120 when game starts..
         print ("Difference: " + difference);
         float tinpaneltimer = (float)dt.TotalSeconds;
         print ("Difference is correct DateTime Checked");
         return (float)difference.TotalSeconds;
 
     }
 
     public void SaveDate()
     {
         print ("Old Date Set");
         PlayerPrefs.SetString (saveLocation, System.DateTime.Now.ToBinary ().ToString ());
         print ("Saving this date to player prefs: " + System.DateTime.Now);
     }
 
     public void ExitButton()
     {
         Panel1.gameObject.SetActive (false);
         Panel2.gameObject.SetActive (false);
         Panel3.gameObject.SetActive (false);
         LevelBar.gameObject.SetActive (true);
     }
 
     public void ClickSlot1()
     {
         Panel1.gameObject.SetActive (true);
         LevelBar.gameObject.SetActive (false);
     }
     public void ClickSlot2()
     {
         Panel2.gameObject.SetActive (true);
         LevelBar.gameObject.SetActive (false);
     }
     public void ClickSlot3()
     {
         Panel3.gameObject.SetActive (true);
         LevelBar.gameObject.SetActive (false);
     }
         
 
 
 
     void Update()
     {
         
         if (tinpaneltimer == 0) {
             print ("Tinpaneltimer finished completing craft");
             StartC3 ();
         }
         if (tinpaneltimerOn == true) {
             
             CheckDate ();
             tinpaneltimer -= Time.deltaTime;
             print ("TinpaneltiemrOn so checking date everyframe and subtracting 1 from 120 timer every time.deltatime");
         }
 
         //Maybe either have a long time for bigger items or just not and have same craft time.
         SlotSteelText.text = "" + Steel;
         SlotElectrumText.text = "" + Electrum;
         SlotTinPanelText.text = "" + TinPanel;
         if (CraftSteelOn == true) {
             
             CraftSteelButtonImage.fillAmount += 1f / 2f * Time.deltaTime;
             CraftSteelButtonImageRight.fillAmount += 1f / 2f * Time.deltaTime;
         }
         if (CraftElectrumOn == true) {
 
             CraftElectrumButtonImage.fillAmount += 1f / 2f * Time.deltaTime;
             CraftElectrumButtonImageRight.fillAmount += 1f / 2f * Time.deltaTime;
         }
         if (CraftTinPanelOn == true) {
 
             CraftTinPanelButtonImage.fillAmount += 1f / (120 - tinpaneltimer) * Time.deltaTime;
             CraftTinPanelButtonImageRight.fillAmount += 1f / (120 - tinpaneltimer) * Time.deltaTime;
         }
         //Store Date.Time when you start the craft and keep checking it until the subtraction is equal to variable
         SteelAmount.text = "" + Steel;
         ElectrumAmount.text = "" + Electrum;
         TinPanelAmount.text = "" + TinPanel;
         XP = PlayerPrefs.GetInt ("XP");
         XPCap = PlayerPrefs.GetInt ("XPCap");
         Level = PlayerPrefs.GetInt ("Level");
         XPCapMulitplier = PlayerPrefs.GetInt ("XPCapMultiplier");
 
         Iron = PlayerPrefs.GetInt ("Iron");
         Coal = PlayerPrefs.GetInt ("Coal");
         Steel = PlayerPrefs.GetInt ("Steel");
 
         Silver = PlayerPrefs.GetInt ("Silver");
         GoldOre = PlayerPrefs.GetInt ("GoldOre");
         Electrum = PlayerPrefs.GetInt ("Electrum");
         TinPanel = PlayerPrefs.GetInt ("TinPanel");
         Tin = PlayerPrefs.GetInt ("Tin");
 
 
     }
     public void CraftSteel()
     {
         
         if (Iron >= 1 && Coal >= 1) {
             Audio.Play ();
             CraftSteelOn = true;
         
             XP = XP + 20;
             PlayerPrefs.SetInt ("Level", Level);
             PlayerPrefs.SetInt ("XPCap", XPCap);
             PlayerPrefs.SetInt("XP", XP);
             PlayerPrefs.SetInt ("XPCapMultiplier", XPCapMulitplier);
 
             StartC ();
         }
         if (Iron <= 1 || Coal <= 1) {
             CraftSteelButtonText.text = "CANT AFFORD";
     
         }
     }
     public void CraftElectrum()
     {
 
         if (Silver >= 1 && GoldOre >= 1) {
             Audio.Play ();
             CraftElectrumOn = true;
 
             XP = XP + 20;
             PlayerPrefs.SetInt ("Level", Level);
             PlayerPrefs.SetInt ("XPCap", XPCap);
             PlayerPrefs.SetInt("XP", XP);
             PlayerPrefs.SetInt ("XPCapMultiplier", XPCapMulitplier);
 
             StartC2 ();
         }
         if (Silver <= 1 || GoldOre <= 1) {
             CraftElectrumButtonText.text = "CANT AFFORD";
 
         }
     }
     public void CraftTinPanel()
     {
 
         if (Tin >= 4) {
             
             SaveDate ();
             tinpaneltimerOn = true;
             Audio.Play ();
             CraftTinPanelOn = true;
 
 
             XP = XP + 13;
             PlayerPrefs.SetInt ("Level", Level);
             PlayerPrefs.SetInt ("XPCap", XPCap);
             PlayerPrefs.SetInt("XP", XP);
             PlayerPrefs.SetInt ("XPCapMultiplier", XPCapMulitplier);
 
 
         }
         if (Tin <= 4) {
             CraftTinPanelButtonText.text = "CANT AFFORD";
 
         }
     }
 
     public IEnumerator WaitForView()
     {
         
         CraftSteelButtonText.text = "CRAFTED";
 
         CraftSteelButton.interactable = false;
         yield return new WaitForSeconds (2f);
         PlayerPrefs.SetInt ("Iron", PlayerPrefs.GetInt ("Iron") - 1);
         PlayerPrefs.SetInt ("Coal", PlayerPrefs.GetInt ("Coal") - 1);
         PlayerPrefs.SetInt ("Steel", PlayerPrefs.GetInt ("Steel") + 1);
         CraftSteelOn = false;
         CraftSteelButtonImage.fillAmount = 0;
         CraftSteelButtonImageRight.fillAmount = 0;
         CraftSteelButton.interactable = true;
         CraftSteelButtonText.text = "CRAFT";
     }
 
     public IEnumerator WaitForView2()
     {
 
         CraftElectrumButtonText.text = "CRAFTED";
 
         CraftElectrumButton.interactable = false;
         yield return new WaitForSeconds (2f);
         PlayerPrefs.SetInt ("Silver", PlayerPrefs.GetInt ("Silver") - 1);
         PlayerPrefs.SetInt ("GoldOre", PlayerPrefs.GetInt ("GoldOre") - 1);
         PlayerPrefs.SetInt ("Electrum", PlayerPrefs.GetInt ("Electrum") + 1);
         CraftElectrumOn = false;
         CraftElectrumButtonImage.fillAmount = 0;
         CraftElectrumButtonImageRight.fillAmount = 0;
         CraftElectrumButton.interactable = true;
         CraftElectrumButtonText.text = "CRAFT";
     }
 
     public IEnumerator WaitForView3()
     {
 
         CraftTinPanelButtonText.text = "CRAFTED";
         yield return new WaitForSeconds (1f);
         CraftTinPanelButton.interactable = false;
         PlayerPrefs.SetInt ("Tin", PlayerPrefs.GetInt ("Tin") - 4);
         PlayerPrefs.SetInt ("TinPanel", PlayerPrefs.GetInt ("TinPanel") + 1);
         tinpaneltimer = 120;
         CraftTinPanelOn = false;
         tinpaneltimerOn = false;
         CraftTinPanelButtonImage.fillAmount = 0;
         CraftTinPanelButtonImageRight.fillAmount = 0;
         CraftTinPanelButton.interactable = true;
         CraftTinPanelButtonText.text = "CRAFT";
     }
 
 
 
     void StartC()
     {
         StartCoroutine (WaitForView ());
     }
 
     void StartC2()
     {
         StartCoroutine (WaitForView2 ());
     }
 
     void StartC3()
     {
         StartCoroutine (WaitForView3 ());
     }
 
 }
 

All the print statements print what they should but the timer is not changed even though the difference is correct. Am I not setting the tinpaneltimer properly? "float tinpaneltimer = (float)dt.TotalSeconds;" line of code should set the timer to the difference between the old date and the current date in seconds but it doesnt do anything. The timer starts up again instead of being off when i restart the game but the timer amount is reset back to 120. Please help I think i am not setting the timer properly.

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 JackTenSeven · Aug 17, 2016 at 07:02 AM 0
Share

I'd like to say a few things. First holy crap there is 224 people following this question, welcome, I see like me you are probably trying to make a build/craft timer like in Clash of Clans or basically in any game. Secondly if you are following and have any and I mean ANY ideas/thoughts/or suggestions that you think might be helpful be sure to comment them because if we work together we can figure this out. -TenSeven

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jdean300 · Aug 17, 2016 at 07:51 AM

 float tinpaneltimer = (float)dt.TotalSeconds;

That is creating a new local variable inside the CheckDate method. What you probably want is:

 tinpaneltimer = (float)dt.TotalSeconds;

 
Comment
Add comment · Show 2 · 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 JackTenSeven · Aug 17, 2016 at 07:52 AM 0
Share

Thanks for the response I'll try it out

avatar image JackTenSeven · Aug 17, 2016 at 08:06 AM 0
Share

Yes this worked! Thanks a ton.

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

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

Clock doesn't stop when reaches 0.0. Can you help me with this. 1 Answer

I try to add a timer pickup to my countdowmTimer the trigger its work and destroy the object but no time add 1 Answer

Project Spark to C# - Started To, While, and No Longer 1 Answer

Traffic Light won't change colors State machine 0 Answers

Variable not resetting on keyup 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