Question by
lokvisnu · Mar 14, 2018 at 03:45 AM ·
timetimer-scriptdatetime
I need a 2 hour realtime timer for my game
private int Gems;
public GameObject Main;
private int Coins;
public GameObject NoEnough;
private string B;
public GameObject BuyManagerObject;
public Text TimeText;
public float mstoWait;
public GameObject Bo;
private string Booster;
private ulong lastbooster;
private void Start()
{
//lastbooster = ulong.Parse(PlayerPrefs.GetString("LastBooster"));
Booster = PlayerPrefs.GetString("BoosterAct");
lastbooster = ulong.Parse (PlayerPrefs.GetString("LastBooster"));
if (!IsChestReady())
{
Main.SetActive(true);
PlayerPrefs.SetString("BoosterAct", "true");
}
}
private bool IsChestReady()
{
ulong diff = ((ulong)DateTime.Now.Ticks - lastbooster);
ulong m = diff / TimeSpan.TicksPerMillisecond;
float SecondsLeft = (float)(mstoWait - m) / 1000.0f;
if (SecondsLeft < 0)
return true;
return false;
}
public void Gems450(){
Gems = PlayerPrefs.GetInt("NoofDiamond");
if(Gems <= 450)
{
Gems -= 450;
PlayerPrefs.SetInt("NoofDiamond", Gems);
lastbooster = (ulong)DateTime.Now.Ticks;
PlayerPrefs.SetString("LastBooster",lastbooster.ToString());
PlayerPrefs.SetString("BoosterAct", "true");
PlayerPrefs.SetInt("BoosterTimer", 7200);
PlayerPrefs.SetInt("Type",1);
Bo.SetActive(false);
Main.SetActive(true);
Set();
}
else
{
NoEnough.SetActive(true);
}
}
public void OK()
{
PlayerPrefs.SetInt("N",0);
}
private void Update()
{
Booster = PlayerPrefs.GetString("BoosterAct");
if (Booster == "true")
{
ulong diff = ((ulong)DateTime.Now.Ticks - lastbooster);
ulong m = diff / TimeSpan.TicksPerMillisecond;
float SecondsLeft = (float)(mstoWait - m) / 1000.0f;
string r = "";
//hour
r += ((int)SecondsLeft / 3600).ToString() + "h ";
SecondsLeft -= ((int)SecondsLeft / 3600) * 3600;
//minutes
r += ((int)SecondsLeft / 60).ToString("00") + "m ";
//Seconds
r += (SecondsLeft % 60).ToString("00") + "s ";
TimeText.text = r;
PlayerPrefs.SetString("BoosterAct","true");
Booster = PlayerPrefs.GetString("BoosterAct");
Bo.SetActive(false);
Main.SetActive(true);
if (IsChestReady())
{
PlayerPrefs.SetString("BoosterAct", "false");
}
}
else
{
Bo.SetActive(true);
Main.SetActive(false);
}
}
public void Set()
{
mstoWait = PlayerPrefs.GetInt("BoosterTimer");
Debug.Log(mstoWait);
Main.SetActive(true);
}
This was the code I used but It Didnt woark properly. The Trouble Is caused while the scene Starts. The variable 'SecondsLeft ' Gets back to zero. Can Some One Help Me please. Thank you
Comment
Your answer
Follow this Question
Related Questions
Alarm Clock Using System Time 0 Answers
How to record lap time for player and AIs? 0 Answers
Allow player to Press Input.GetKeyDown once per update ? 0 Answers
DateTime.Today for Different Time Zone 1 Answer
Milliseconds Timer Question 1 Answer