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 /
avatar image
0
Question by carter-carl30 · Jun 17, 2017 at 06:48 AM · c#activatetime.timescale

Help with reward player after 24 hours has passed (save time on exit, read on launch)

Hi all, I am trying to add some kind of daily reward to my players.

What I want to do is when a player plays the game then quits it saves the time on quit.

When the player re launches the game check if 24 hours has passed and if so activate my reward gameobject to reward the player 500 coins.

I currently have this script on a gameobject on my title screen:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class DailyReward : MonoBehaviour {
 
     DateTime startTime;
     TimeSpan currentTime;
     //TimeSpan oneDay = new TimeSpan(24, 0, 0);
     TimeSpan oneDay = new TimeSpan(0, 2, 0);
     public GameObject reward;
 
     void Start()
     {
         StartTime();
     }
     
     void Update()
     {
         currentTime = DateTime.UtcNow - startTime;
         if (currentTime >= oneDay)
         {
             //set daily reward to active to give player 500 coins
             reward.SetActive(true);
 
             StartTime();
         }
     }
     
     private void StartTime()
     {
         startTime = DateTime.UtcNow;
     }
     
     //Returns a string with currentTime in a 24:00:00 format
     private string GetTime(TimeSpan time)
     {
         TimeSpan countdown = oneDay - time;
         return countdown.Hours.ToString() + ":" + countdown.Minutes.ToString() 
             + ":" + countdown.Seconds.ToString();
     }
 }

and a reward gameobject that has a script on to reward the player if its activated.

As you can see I have changed it to 2 minutes just to test for now, I don't need any cheat prevention eg user changing their device time, thats up to them if they want to cheat!

At the moment when I play and quit, then relaunch after 2 minutes have passed nothing happens.

Can anyone please help me?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Woltus · Jun 17, 2017 at 09:47 AM

First of all don't reset you'r start time at every game start. Now if player starts a game every hour he will never get that prize.

You need to store that time between game sessions. Try to use PlayerPrefs : https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

And a little tip, 22 or 23 hours till reward should be better ;)

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 carter-carl30 · Jun 17, 2017 at 04:37 PM 0
Share

I am trying to save the current time to playerprefs

I have made a script on my quit button

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class SaveTime : $$anonymous$$onoBehaviour {
 
     
 void Start()
     {
         //currentTime = current system time
         currentTime = DateTime.UtcNow;
 
         //Save the current system time to player prefs
         PlayerPrefs.SetInt("timesaved", currentTime);
     }
 }

Then on my daily reward script I am trying to read the saved playerpref saved time

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class DailyRewardNew : $$anonymous$$onoBehaviour {
 
     DateTime startTime;
     TimeSpan currentTime;
     
     //amount of time to reward   Hrs, $$anonymous$$in, Secs
     TimeSpan oneDay = new TimeSpan(23, 0, 0);
 
     //the reward object
     public GameObject reward;
 
     void Start()
     {
 
         //NEED TO CHEC$$anonymous$$ PLAYER PREFS SET AT GA$$anonymous$$E QUIT AND SET THAT AS STARTTI$$anonymous$$E
 
         //get saved time from last game from playerprefs
         startTime = PlayerPrefs.GetInt("timesaved");
 
         //currentTime $$anonymous$$us startTime
         currentTime = DateTime.UtcNow - startTime;
 
         //if currentTime is greater than oneDay(currently set at 23hrs)
         if (currentTime >= oneDay)
         {
             //set daily reward to active to give player 500 coins
             reward.SetActive(true);
         }
     }
 }

I am getting this error: error CS0029: Cannot implicitly convert type int' to System.DateTime'

do I need to save the playerpref as something else? eg float?

avatar image carter-carl30 · Jun 21, 2017 at 10:57 AM 0
Share

Anyone help me please?

avatar image
0

Answer by FantacyfilmsUS · Jun 21, 2017 at 11:13 AM

Instead of using an integer for your variables, use a string in whatever script in gives you CS0029. Find out where its trying to convert your time into an int and try things like .toString:

http://answers.unity3d.com/questions/201190/timetime-as-int-value-.html

this might be helpful...

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 carter-carl30 · Jun 23, 2017 at 05:05 PM 0
Share

Thankyou for your reply, I have tried this below, on my daily reward script:

 using UnityEngine;
  using System.Collections;
  using System;
  
  public class DailyRewardNew : $$anonymous$$onoBehaviour {
  
      DateTime startTime;
      TimeSpan currentTime;
      
      //amount of time to reward   Hrs, $$anonymous$$in, Secs
      TimeSpan oneDay = new TimeSpan(23, 0, 0);
  
      //the reward object
      public GameObject reward;
  
      void Start()
      {
  
          //NEED TO CHEC$$anonymous$$ PLAYER PREFS SET AT GA$$anonymous$$E QUIT AND SET THAT AS STARTTI$$anonymous$$E
  
          //get saved time from last game from playerprefs
          startTime = PlayerPrefs.GetString("timesaved");
  
          //currentTime $$anonymous$$us startTime
          currentTime = DateTime.UtcNow - startTime;
  
          //if currentTime is greater than oneDay(currently set at 23hrs)
          if (currentTime >= oneDay)
          {
              //set daily reward to active to give player 500 coins
              reward.SetActive(true);
          }
      }
  }

on my savetime script (on quit game):

  using UnityEngine;
  using System.Collections;
  using System;
  
  public class SaveTime : $$anonymous$$onoBehaviour {
  
  void Start()
      {
          //Save the current system time to player prefs
          PlayerPrefs.SetFloat("timesaved", DateTime.UtcNow).ToString("0");
  
      }
  }

getting this error: error CS0029: Cannot implicitly convert type string' to System.DateTime'

what am I doing wrong? Can someone tell me the correct steps to saving and reading DateTime.UtcNow please? it's very confusing!

avatar image carter-carl30 · Jun 24, 2017 at 06:48 PM 0
Share

I've been looking all over trying to suss this out, I came across this video:

https://www.youtube.com/watch?v=VQ0Ph7U$$anonymous$$RiU∈dex=9&list=PLbCx65TBvT-QgTitV$$anonymous$$CWGH1HQW_Yfd$$anonymous$$D$$anonymous$$

I have been trying to work it out to fit my code, I now have this below:

DailyRewardNew script:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class DailyRewardNew : $$anonymous$$onoBehaviour {
     
     DateTime startTime;
     TimeSpan currentTime;
     
     //amount of time to reward   Hrs, $$anonymous$$in, Secs
     //TimeSpan oneDay = new TimeSpan(23, 0, 0);
     
     //the reward object
     public GameObject reward;
     
     void Start()
     {
         
         //NEED TO CHEC$$anonymous$$ PLAYER PREFS SET AT GA$$anonymous$$E QUIT AND SET THAT AS STARTTI$$anonymous$$E
 
         startTime = Convert.ToDateTime (PlayerPrefs.GetString("timesaved"));
 
         //currentTime $$anonymous$$us startTime
         currentTime = DateTime.Now - startTime;
 
         Debug.Log ("timepassed:" +currentTime);
 
         //if currentTime is greater than oneDay(currently set at 23hrs)
         if (currentTime.Hours >= 23)
         {
             //set daily reward to active to give player 500 coins
             reward.SetActive(true);
         }
     }
 }

SaveTime script:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class SaveTime : $$anonymous$$onoBehaviour {
     
     void Start()
     {
         //Save the current system time to player prefs
         PlayerPrefs.SetString("timesaved",GetStringTime());
 
         Debug.Log ("time" + GetStringTime());
     }
 
     string GetStringTime(){
 
         DateTime now = Convert.ToDateTime(DateTime.Now);
 
         return now.$$anonymous$$onth + "/" + now.Day +"/" + now.Year +"/" + now.Hour +":" + now.$$anonymous$$inute +":" + now.Second +":";
     }
 }

If I manually activate my savetime object I get a date eg 6/24 19:46:10 displayed in console.

If I start my game, therefore running the DailyRewardNew script, I get this error:

FormatException: String was not recognized as a valid DateTime. System.DateTime.Parse (System.String s, IFormatProvider provider, DateTimeStyles styles) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/DateTime.cs:924) System.DateTime.Parse (System.String s, IFormatProvider provider) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/DateTime.cs:912) System.DateTime.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/DateTime.cs:907) System.Convert.ToDateTime (System.String value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:719) DailyRewardNew.Start () (at Assets/Scripts/cozscripts/DailyRewardNew.cs:21)

I think it's something to do with this https://stackoverflow.com/questions/44031297/string-was-not-recognized-as-a-valid-datetime-exception

but i'm stumped again :( can anyone please help me?

avatar image
0

Answer by wtdunity · Jan 13, 2018 at 05:45 AM

public Text txt_timer; public GameObject RewardButton; void Start () { StartCoroutine(CheckTimer()); }

 public void GetReward()
 {
     DateTime CurrentTime = DateTime.Now.AddMinutes(1440);
     CurrentTime.SaveDate();

     StopAllCoroutines();
     StartCoroutine(CheckTimer());
 }


 public IEnumerator CheckTimer()
 {
     DateTime SavedTime = DateTimeExtension.GetSavedDate();
     TimeSpan RemaningTime = SavedTime.Subtract(DateTime.Now);

     if (RemaningTime.TotalMinutes > 0) 
     {
         RewardButton.SetActive(false);
     }
     else
     {
         RewardButton.SetActive(true);
         txt_timer.text ="Collect Reward!!";
     }
         
     while (RemaningTime.TotalMinutes>0)
     {            
         RemaningTime =SavedTime.Subtract( DateTime.Now);
         txt_timer.text ="Wait For Reward \n"+ RemaningTime.Hours + ":" + RemaningTime.Minutes + ":" + RemaningTime.Seconds;
         yield return new WaitForSeconds(1f);
     }

     if (RemaningTime.TotalMinutes <= 0) 
     {
         RewardButton.SetActive(true);
         txt_timer.text ="Collect Reward!!";
     }
     Debug.Log("StopCoroutine");

 }

}

public static class DateTimeExtension { public static void SaveDate(this DateTime _date,string Key="SavedDate") { string d = Convert.ToString(_date); PlayerPrefs.SetString(Key, d); } public static DateTime GetSavedDate(string key = "SavedDate") { if (PlayerPrefs.HasKey("SavedDate")) { string d = PlayerPrefs.GetString("SavedDate"); return Convert.ToDateTime(d); } else { return DateTime.Now; } }

}

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 Meet_Sevak · Mar 28, 2020 at 11:12 PM 0
Share

Hey, I have a doubt, like where do call this get reward() function!! I mean on a button or else on some other script!! Thanks....

@wtdunity

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Pause game while GUI instructions displayed? 1 Answer

activate other objects inside a different objects script 2 Answers

Can't set Time.timeScale back to 1. 2 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