I have a script that manages an energy system that replenishes every once in awhile. This code works fine for the exe and in the editor but will not run properly on android. The only functions that works as intended are those that don’t involve time.Calling the functions from another script also did not work. Is there any way to fix this? Is DateTime.Now not compatible with android?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System;
public class Survivors : MonoBehaviour
{
float rechargeTime;
DateTime currentTime;
DateTime lastChecked;
DateTime lastRefill;
int elapsedHour,elapsedMin;
int survivorsToAdd;
// Use this for initialization
void Awake ()
{
makeUpForLostTime();
checkSurvivors();
}
// Update is called once per frame
void Update ()
{
checkSurvivors();
}
public void Ok()
{
GameObject.Find("NMS_Canvas").gameObject.SetActive(false);
}
public void checkSurvivors()
{
GameObject temp = GameObject.Find("Energy");
temp.GetComponent<Text>().text = "x " + PlayerPrefs.GetInt("currSurvivors") + "/" + PlayerPrefs.GetInt("maxSurvivors");
if (PlayerPrefs.GetInt("currSurvivors")> PlayerPrefs.GetInt("maxSurvivors"))
{
temp.GetComponent<Text>().text = "x " + PlayerPrefs.GetInt("maxSurvivors") + "/" + PlayerPrefs.GetInt("maxSurvivors");
}
}
IEnumerator compareTime()
{
yield return new WaitForSeconds(1f);
currentTime = DateTime.Now;
lastRefill = Convert.ToDateTime(PlayerPrefs.GetString("lastRefill"));
TimeSpan difference = currentTime - lastRefill;
if (difference.Minutes >= PlayerPrefs.GetInt("SurvivorRefreshTime") && PlayerPrefs.GetInt("currSurvivors") <= PlayerPrefs.GetInt("maxSurvivors"))
{
PlayerPrefs.SetString("lastRefill", System.DateTime.Now.ToString());
PlayerPrefs.SetInt("currSurvivors", PlayerPrefs.GetInt("currSurvivors") + 1);//add one survivor
Debug.Log("Add one survivor");
}
Debug.Log(difference);
int seconds = (59 - difference.Seconds);
string secondsText=seconds.ToString();
if (seconds<10)
{
secondsText = "0" + seconds;
}
GameObject.Find("More in").GetComponent<Text>().text = "More In: " + (PlayerPrefs.GetInt("SurvivorRefreshTime")+ -difference.Minutes-1) + ":" + secondsText;
StartCoroutine(compareTime());
}
public void makeUpForLostTime()
{
lastChecked = Convert.ToDateTime(PlayerPrefs.GetString("lastRecordedTime"));
Debug.Log(lastChecked);
DateTime currTime = DateTime.Now;
Debug.Log(currTime);
TimeSpan difference = currTime - lastChecked;
Debug.Log(difference);
elapsedHour = difference.Hours;
elapsedMin = difference.Minutes;
while (elapsedHour>0)
{
elapsedMin += 60;
elapsedHour--;
}
if (elapsedMin> PlayerPrefs.GetInt("SurvivorRefreshTime")&& elapsedMin>0)
{
survivorsToAdd = elapsedMin / PlayerPrefs.GetInt("SurvivorRefreshTime");
}
int tempCurr = PlayerPrefs.GetInt("currSurvivors");
int tempMax = PlayerPrefs.GetInt("maxSurvivors");
while ( tempCurr <= tempMax && survivorsToAdd>0)
{
tempCurr++;
Debug.Log("Making up for lost time");
survivorsToAdd--;
}
PlayerPrefs.SetInt("currSurvivors", tempCurr);
StartCoroutine(compareTime());
}
}