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.
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: