how can i make certain values continue when the game/app is closed?

certain big incremental games such as adventure capitalist do this, and i am wondering if there is a way to implement that in code through unity as well? i know there is a "DontDestroyOnLoad" command, but I am un sure if there are any kinds of "DontDestroyOnClose Or Load" type of commands. Thanks!

What exactly are you talking about? Keeping things running after the program has quit? That's not possible. The usual way to "calculate" things in the background (say, "earning money even while your computer is off") is to just calculate them when the program starts. For that you just need to save the date and time when the program exits, and calculate the difference at start.

oh ok thanks, so basically i just do that, and then whatever amount they were earning when they logged off i calculatei t at the start. could you give me an example of how i might save date and time then call it at the start up ?

Unity has a PlayerPrefs that is intended for this purpose:

1 Like

The easiest way I can think of is to just take the current time and date in, say, seconds since the epoch, store that into PlayerPrefs, and then at the start of your program, do the same, and calculate the time difference from that:

// on quit
long date = DateTime.UtcNow.Ticks / 10000 / 1000;
PlayerPrefs.SetInt("lastDate", date);

// on start
long nowDate = DateTime.UtcNow.Ticks / 10000 / 1000;
long lastDate = PlayerPrefs.GetInt("lastDate");
long secondsSinceLastDate = nowDate - lastDate;

I'm not sure if PlayerPrefs.SetInt() is big enough to actually hold the value. You might want to use bigger units than seconds, or use something like this to use a later epoch date:

1 Like

awesome, thank you, i will work with this and see what i can figure out. Aslo thank you wetcircuit for the link on player prefs. for some reason i am having a hard time understanding how to "call " the saved player prefs on opening the game. I will see if i can get it re watching the unity video again this time. thanks! hopefully this will help me work it out now.

1 Like

You don't need to load the PlayerPrefs data. The class does it on its own.

oh ok, so if i put in something like this..

    void OnDestroy()
        PlayerPrefs.SetFloat ("FISH",;
        PlayerPrefs.SetFloat ("FISHPERTAP", FishClick.fishPerTap);
            PlayerPrefs.Save ();

then that should save when i close the game automatically for the 2 values fish and fishpertap ?

I think it should work.

cool, thanks! ill play around with that and see :) going to build the project now and then run it and see if it saves thanks!

doesnt seem to be saving, any idea what i would need to change ? for some reason this using player prefs to save thing has been tricky for me to work out. doesnt seem like it is too tricky from all the videos ive watched though

[quote=“Sylvir”, post:11, topic: 577524]
doesnt seem to be saving, any idea what i would need to change ?
There is a free utility on the Asset Store that allows you to create, modify, and delete PlayerPrefs for your project directly in the editor. I tried running the code snippet you put above (replacing the variable names with actual values) and they showed up in the asset’s editor window (Window → PlayerPrefs Overview).!/content/31230

thanks ill give that a look see and try and work out what im missing

how do i call it correctly to load when i start again? think thats where im doing something wrong

Load the values from the prefs? = PlayerPrefs.GetFloat("FISH");
FishClick.fishPerTap = PlayerPrefs.GetFloat("FISHPERTAP");

thank you