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 /
This question was closed Aug 26, 2015 at 12:36 PM by Political Peace Party Studios for the following reason:

Other

avatar image
1
Question by Political Peace Party Studios · Aug 26, 2015 at 06:49 AM · c#timeserverwwwpersistence

Getting a Code to Run at a Specific Time of Day Even if App is Off.

Hello, I am using the code below in order to query the correct time on a server. Its a 24 hour timer that never stops. It is working perfectly and I am displaying the correct time on Unity editor with no errors. Except, the problem is I need this timer to run a specific code at exactly 24.00:00 every single day - I mean it works, except you have to be on the app running the scene in order for it to work, which makes sense. How do I get it to run the code at 24.00:00 even if the app is off?

For Example - Lets say you press a button at 21.00:00 and the button disappears. And the only way for the button to reappear is if the timer passes 24:00:00. With the code below you would have to be on the app on the scene at exactly 24.00:00 to turn the button back on. How do I get the button to reappear, when it passes 24.00:00 even if the app was off. So if they get on at 2.00:00 the button is back on and if they press the button again it disappears and they have to wait until the timer hits 24.00:00 for the button to turn back on again but they don't have to have the app open.

     using UnityEngine;
     using System.Collections;
     using UnityEngine.UI;

      public class Timer : MonoBehaviour {

 public Text myText;
 string url = "http://www.website.com/gettime.php";
 
 void Update () {

     WWW www = new WWW(url);
     StartCoroutine(WaitForRequest(www));
 }

 IEnumerator WaitForRequest(WWW www)
 {
     yield return www;
     // check for errors
     if (www.error == null)
     {
         Debug.Log("WWW Ok!: " + www.data);
         myText.text = www.data;

         if (www.data == "24.00:00") {


             Debug.Log("CODE I NEED TO RUN");

         }


     } else {
         Debug.Log("WWW Error: "+ www.error);
     }    
  }
 
   }
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
2

Answer by fafase · Aug 26, 2015 at 07:14 AM

You cannot. Imagine if any developer could start his app without your agreement.

What you can do is store the time when the user quits and when he comes back compare the elapsed time. If more than x min/s/h/days then perform appropriate action, in your case show the button.

Or you can send notification which is the way most do. You know those annoying bling in the middle of the night to let you know you have refill your heart. But even for those a user may not allow them.

Comment
Add comment · Show 6 · 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 Political Peace Party Studios · Aug 26, 2015 at 07:26 AM 0
Share

"What you can do is store the time when the user quits and when he comes back compare the elapsed time. If more than x $$anonymous$$/s/h/days then perform appropriate action, in your case show the button."

Yes, this is what I am looking for. Can you be a little more specific with how to implement this in my code?

avatar image fafase · Aug 26, 2015 at 08:10 AM 0
Share
 void Awake(){
      CheckTimer();
 }
 private void CheckTimer(){
      // Check if player prefs contains QuitTime key
      string time =PlayerPrefs.GetString("QuitTime");
     // Convert to DateTime
     // Compare and do action
 }
 void OnApplicationPause(bool pause) {
        if(pause == false) CheckTimer();
     }
 void OnApplicationQuit(){
      PlayerPrefs.SetString("QuitTime", DateTime.Now.ToString());
 }
avatar image Political Peace Party Studios · Aug 26, 2015 at 08:27 AM 0
Share

The reason I went with checking the time on a server is so my app could be secure. Player prefs are easy to hack don't you think?

avatar image fafase · Aug 26, 2015 at 08:36 AM 1
Share

Then OnQuit you send the data to the server. And on resume you compare.

avatar image Political Peace Party Studios · Aug 26, 2015 at 08:45 AM 0
Share

Any advice on how I would be able to save it to a server?

Show more comments

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

25 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

Related Questions

`Recv failure: Connection was reset` In editor? (platform set to Android NOT WebPlayer) 0 Answers

Why is my upload/download WebRequest not working? 1 Answer

WWW/Server Problems 0 Answers

Download images from server and storing it in Resources. 1 Answer

How to get multiple variables from page on server? 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