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?

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 : Unity - Scripting API: PlayerPrefs

And a little tip, 22 or 23 hours till reward should be better :wink:

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:

this might be helpful…

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 

"+ 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;
}
}

}