Need help with incremental game saving scripting.

Hello there, I’m making an incremental game that can be played on android. I’ve tried a couple of time’s to make it work so it saves my game everytime i close it and when i open it again every stats are still there. But i can’t get it to work.

Here’s a video of the game so far, i would like to save how much gold i have, how much gold per/click , how much gold per second, and what level the items are so you won’t need to buy it over and over again every time you play the game and quit the game.

Here are my scripts:

Click: Click Script - Pastebin.com

CurrencyConverter: CurrencyConverter - Pastebin.com

GoldPerSec: GoldPerSec - Pastebin.com

Itemmanager: ItemManager - Pastebin.com

UpgradeManager: UpgradeManager - Pastebin.com

Thank you for you’re time , if you can help me please do :).

Greetz. Bjarne

You can save things with PlayerPrefs. Something along the lines of…

int Gold = 100000000000;

void SaveGame (){

      //Save the gold
      PlayerPrefs.SetInt ("GoldAmount", Gold);

}

void Start(){

//And when the game starts again, retrieve the gold
Gold = PlayerPrefs.GetInt ("GoldAmount");

}

void OnApplicationQuit() {
        SaveGame ();
        PlayerPrefs.Save();
}

Hope this helps :slight_smile:

Whenever i use PlayerPrefs.SetInt
or PlayerPrefs.GetInt it turns red in monodevelop, do you know why this is?
https://gyazo.com/46c73e4ff843e40fde116a59c1254f19

You also need to specify an amount, currently you are only specifying a name.

Do keep in mind though, that PlayerPrefs is NOT recommended to use to save anything that should not be editable by the user outside of the game as playerprefs saves a cleartext file. If you do not want your players to be able to edit the file easily, I suggest looking into serializing your data. More info in this live training VOD: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

When you set the playerpref int it needs two parameters, the Internet name & the value. Yours should probably be:
PlayerPrefs.SetInt(“GoldAmount”,gold)

Where gold is the int you defined at the top of your script. You should also probably call your script something else as it is a reserved namespace so might be causing the issue you have as it could be getting confused between your script & the actual Unity playerprefs.

1 Like

:open_mouth: Completely missed that, nice catch

I can’t get it to work , will try again tomorrow, hopefully then i will be able to do it

The sample code provided above needs you to call the save function. If you are just trying to get it to save without the player hitting a save button then every time something changes you will need to set the player prefs & also save the player prefs. I have no PC atm but I think there is something like PlayerPrefs.Save as well that you need to use.

If you want it to save when you leave the game, you can call the SaveGame function in OnApplicationQuit.

    void OnApplicationQuit() {
     SaveGame ();
     PlayerPrefs.Save();
}

I’ve added that to the code above too :slight_smile:

If this solves the OP’s problem then Unity really needs to update their documentation as according to the save documentation:

i just can’t get it to work , i’m giving up , maybe i’m not experienced enough maybe its because i’m just 13 :stuck_out_tongue: , but i don’t now how to get it to work , but thank everyonne for trying to help me out

It’s not efficient, but did you try saving playerprefs every time you updated them rather than calling a function?