So what I need to do is that , when the player opens the app and has bought a manager , the game needs to give the money that the player should have earned during the idle time.
That’s my first script (that script should give the player money :
//Check if app is closed
var appQuitted : boolean;
var secondsStatic : float;
//Get other script functions
var thisObject : GameObject;
var buttonsScript : Cooldowns;
buttonsScript = thisObject.GetComponent(Cooldowns);
//Another script
var thisObjectTime : GameObject;
var getPassedSeconds: WeedTime;
getPassedSeconds = thisObjectTime.GetComponent(WeedTime);
function Update()
{
if (appQuitted == true)
{
CheckManagers();
appQuitted = false;
secondsStatic=getPassedSeconds.secondsPassedWeed;
}
}
function OnMouseDown ()
{
appQuitted=true;
}
function LoopCheck ()
{
if (getPassedSeconds.secondsPassedWeed >= buttonsScript.actionTime)
{
CheckManagers();
}
}
function CheckManagers ()
{
if (secondsStatic >= buttonsScript.actionTime)
{
secondsStatic-=buttonsScript.actionTime; //substracts the action time (for example 5) from the secondsStatic
Debug.Log("givemoney");
LoopCheck(); //checks if secondsStatic >= buttonsScript.actionTime
}
else if (getPassedSeconds.secondsPassedWeed < buttonsScript.actionTime)
{
buttonsScript.actionTime+=getPassedSeconds.secondsPassedWeed; //adds the idle seconds to the actionbar
}
}
That’s my offline time checker :
var secondsPassedWeed : double;
function Save()
{
var dateTimeString = System.DateTime.UtcNow.ToBinary().ToString();
PlayerPrefs.SetString ("DateTime", dateTimeString);
}
function Load()
{
var dateTimeLong : long;
var didParseLong = System.Int64.TryParse (PlayerPrefs.GetString ("DateTime"),dateTimeLong);
if (didParseLong)
{
var savedDateTime = System.DateTime.FromBinary (dateTimeLong);
var now = System.DateTime.UtcNow;
var timeSpan = now - savedDateTime;
secondsPassedWeed = timeSpan.TotalSeconds;
}
}
What’s wrong with that?
In the first script the player need to actually click a button to start the timer and then exit from the game (I’ve done that because OnApplicationQuit does not work in-editor , it was easier to check if my script works).
If you have another way to do that I will be happy
-----MORE INFO-----
I need a script that checks if the idle time >= the time to complete an action (for example 5) , if it’s > then give money and substract the time to complete an action (5) and check one more time if idle time > the time to complete an action.
If the idle time < the time to complete an action then add the idle time to the action progress to complete (for example if the idle time is 5hours and the action time complete is 10 hours then add 5 hours to the 10 hours bar , it will be half completed).
That’s how my script should work but it doesn’t , it crashes.