Reputation: 61
I want to create a 24 hour rewarding lock in my game(unity3d) I want to reward a user for 24 hour after 24 hour the game object will be locked for another 24 hours how would i do it in unity ???
Upvotes: 0
Views: 1310
Reputation: 145
When the player 'uses' the object for the first time, you can save the object and the current time:
PlayerPrefs.SetString('<objectnametimer>', DateTime.Now.AddHours(25).ToString());
This will save the new datetime, so everytime the game boots up, you can check things like this:
void Start() {
var unlockDate = DateTime.Parse(PlayerPrefs.GetString('<objectnametimer>'));
if(unlockDate < DateTime.Now) {
//object unlocked again
}
else {
//object still locked, how long you ask?:
TimeSpan diff = unlockDate.Subtract(DateTime.Now);
Debug.Log("object locked for " + diff.Minutes + " more minutes");
}
}
I didn't check for compile errors, (I'm on my phone) But this looks promising to me :)
PS: If you want to create a countdown, just keep iterating the else code and convert it to a nice format using diff like described here
EDIT: when iterating, use coroutines to yield return new WaitForSeconds(1)
for a calculatiosn every 1 second. also make sure to check each time if (unlockedDate < DateTime.Now)
to trigger unlocking :)
Happy coding
Upvotes: 1