Unity crashing , help me with an Adventure Capitalist-like manager

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

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

So if I got this correctly you want to have the time that has passed between the last shutdown of the game and this startup.

I would do this:

using UnityEngine;
using System.Collections;
using System; //This Import is important!

public class CheckDay : MonoBehaviour
{
	//This method will return the hours that have passed since the last shutdown
	public static int getPassedHours (DateTime shutDownTime)
	{
		//shutDownTime = the time when the game was closed. You get it with:
		//shutDownTime = DateTime.Now;
		DateTime nowTime = DateTime.Now;
		int hours = (shutDownTime - nowTime).TotalHours;
		return hours;
	}
}

If you want to get days instead just replace all “hours” with “days”.
Note that you will always need the “using System;” to be able to use these Time-classes
Alternativeley you can also write “System.” infront of your methods:

for example: System.DateTime nowTime = System.DateTime.Now;

Also, sorry its in C#… I dont know Javascript… just try to translate it somehow :stuck_out_tongue:

Did this help? Good luck!