Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by Montassar_Hizem · Aug 12, 2020 at 06:41 PM · unity 2dtimercountdowntimer countdowntimer-script

countdown timer starts from the begining when i reLaunch the app

hi, so i created a countdown timer script wich block the user from playing when he reachs a certain score and the user can play only when the timer reachs 00:00:00 . The problem is that the timer starts from 11:59:59 and when the user exits the app and open it again after a period, the timer starts from 11:59:59 . So, i need the timer to keep running even when the user exits the app. also i need this task to be secured as much as possible so the player can't hack it and bypass the timer. my app is connected to a server if this can help. this is my script :

 private float timeRemaining = 43200;
 public bool timerIsRunning = false;
 public bool getTimeJustOnce = false;
 public Text timerText;

 private void Update()
      {
          int highscore = int.Parse(todayGains.text);
          if(highscore == 1500)
          {
              
  
              startEarningButton.GetComponent<Collider2D>().enabled = false;
              startEarningButton.GetComponent<Animator>().enabled = false;
  
  
              if (timeRemaining > 0)
              {
                  timeRemaining -= Time.deltaTime;
                  DisplayTime(timeRemaining);
                  PlayerPrefs.SetFloat("savedTime", timeRemaining);
  
              }
              else
              {
                  while(timerIsRunning == true)
                  {
                      timerText.text = "";
                      timeRemaining = 0;
                      timerIsRunning = false;
                      startEarningButton.GetComponent<Collider2D>().enabled = true;
                      startEarningButton.GetComponent<Animator>().enabled = true;
                      PlayerPrefs.SetString("todayGains", "0");
                      todayGains.text = "0";
  
                  }
              }
  
          }
  
      }
  
  void DisplayTime(float timeToDisplay)
      {
          timeToDisplay -= 1;
  
          float minutes = Mathf.FloorToInt(timeToDisplay / 60) % 60;
          float seconds = Mathf.FloorToInt(timeToDisplay % 60);
          float hours = Mathf.FloorToInt(timeToDisplay / 3600) % 24;
  
          timerText.text = string.Format("ComeBack After : {0:00}:{1:00}:{2:00}", hours, minutes, seconds) ;
  
      }



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
0

Answer by Spip5 · Aug 13, 2020 at 12:11 AM

You should watch this tutorial to save (via a file) the Time at wich the user does the trigger, and check the remaining time based on the file any time you need. That way it does not matter if the user closes the app or not. Basically it goes like this :

 --> Register 2 variables on user trigger
 
 System.DateTime userTriggerTimer = Time.Now;
 System.DateTime userNextMoveTime = Time.Now.AddDays(X).AddHourse(X).AddMinutes(X).AddSeconds(X);
  
 --> Save a file with these variables (do a System.Serialized class) (watch tutorial)
 --> Load the file when you need to check (watch tutorial) 
 
 --> In a script check if the userNextMoveTime is < Time.Now (which means he is allowed to do an action)

This will 100% work if done correctly, and it is quite hard for a regular user to bypass that solution.

Comment
Add comment · 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 Spip5 · Aug 13, 2020 at 12:15 AM 0
Share

Also, avoid using player prefs except for settings (volume, etc..) as it's super easy to "hack". Use the tutorial i provided to save sensitive data (such as score or money)

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

140 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

Related Questions

Speed up timer 1 Answer

Make a countdown timer say "go" once it reaches 0 1 Answer

Stop and Pause Timer 2 Answers

How do you make a countdown timer that will lose time if you press a button? 1 Answer

countdown timer 0 Answers


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