1
\$\begingroup\$

I'm creating a game which stores a few variables using PlayerPrefs. It saves and loads the PlayerPrefs perfectly fine while testing in the editor, however when I build it to WebGL or Unity Web Player, both of them fail to save or load (I don't know which fails) and WebGL throws an exception on loading. Heres the c# script. Loading and saving are in their own procedures: loadData() and saveData()

public decimal money = 0;
public Text moneyTxt;

public Modal modal;

public AudioSource clickSFX;

private bool hasLoaded = false;

public Prog lemonadeStand;
public Prog newspaperDelivery;
public GameObject newspaperDeliveryBuy;
public Prog carWash;
public GameObject carWashBuy;

void Start () {
    askToLoad ();
}

// Update is called once per frame
void Update () {
    moneyTxt.text = "$" + money.ToString ("F2");

    if (Input.GetButtonDown ("Fire1")) {
        clickSFX.Play();
    }
}

void OnApplicationQuit() {
    if (hasLoaded) {
        saveData ();
    }
}

void saveData () {
    PlayerPrefs.SetString ("SaveMoney", money.ToString ());

    PlayerPrefs.SetFloat ("LemonadeStandMultiplier", lemonadeStand.multiplier);
    PlayerPrefs.SetFloat ("LemonadeStandUpgradeCost", lemonadeStand.upgradeCost);
    PlayerPrefs.SetInt ("LemonadeStandHired", lemonadeStand.hired ? 1 : 0);
    PlayerPrefs.SetFloat ("LemonadeStandPercentage", lemonadeStand.percentage);
    PlayerPrefs.SetFloat ("LemonadeStandTimeMultiplier", lemonadeStand.timeMultiplier);

    if (newspaperDelivery.transform.parent.gameObject.activeSelf) {
        PlayerPrefs.SetFloat ("NewspaperDeliveryMultiplier", newspaperDelivery.multiplier);
        PlayerPrefs.SetFloat ("NewspaperDeliveryUpgradeCost", newspaperDelivery.upgradeCost);
        PlayerPrefs.SetInt ("NewspaperDeliveryHired", newspaperDelivery.hired ? 1 : 0);
        PlayerPrefs.SetFloat ("NewspaperDeliveryPercentage", newspaperDelivery.percentage);
        PlayerPrefs.SetInt ("NewspaperDeliveryBought", newspaperDelivery.bought ? 1 : 0);
        PlayerPrefs.SetFloat ("NewspaperDeliveryTimeMultiplier", newspaperDelivery.timeMultiplier);
    } else {
        PlayerPrefs.SetInt ("NewspaperDeliveryBought", newspaperDelivery.bought ? 1 : 0);
    }

    if (carWash.transform.parent.gameObject.activeSelf) {
        PlayerPrefs.SetFloat ("CarWashMultiplier", carWash.multiplier);
        PlayerPrefs.SetFloat ("CarWashUpgradeCost", carWash.upgradeCost);
        PlayerPrefs.SetInt ("CarWashHired", carWash.hired ? 1 : 0);
        PlayerPrefs.SetFloat ("CarWashPercentage", carWash.percentage);
        PlayerPrefs.SetInt ("CarWashBought", carWash.bought ? 1 : 0);
        PlayerPrefs.SetFloat ("CarWashTimeMultiplier", carWash.timeMultiplier);
    } else {
        PlayerPrefs.SetInt ("CarWashBought", carWash.bought ? 1 : 0);
    }

    PlayerPrefs.Save ();
}

void loadData () {
    money = decimal.Parse(PlayerPrefs.GetString("SaveMoney"));

    lemonadeStand.multiplier = PlayerPrefs.GetFloat ("LemonadeStandMultiplier");
    lemonadeStand.upgradeCost = PlayerPrefs.GetFloat ("LemonadeStandUpgradeCost");
    lemonadeStand.hired = PlayerPrefs.GetInt ("LemonadeStandHired") == 1 ? true : false;
    if (lemonadeStand.hired) { lemonadeStand.enable(); lemonadeStand.percentage = PlayerPrefs.GetFloat ("LemonadeStandPercentage");}
    lemonadeStand.timeMultiplier = PlayerPrefs.GetFloat ("LemonadeStandTimeMultiplier");

    if (PlayerPrefs.GetInt ("NewspaperDeliveryBought") == 1) {
        newspaperDelivery.multiplier = PlayerPrefs.GetFloat ("NewspaperDeliveryMultiplier");
        newspaperDelivery.upgradeCost = PlayerPrefs.GetFloat ("NewspaperDeliveryUpgradeCost");
        newspaperDelivery.hired = PlayerPrefs.GetInt ("NewspaperDeliveryHired") == 1 ? true : false;
        if (newspaperDelivery.hired) {
            newspaperDelivery.enable ();
            newspaperDelivery.percentage = PlayerPrefs.GetFloat ("NewspaperDeliveryPercentage");
        }
        newspaperDelivery.transform.parent.gameObject.SetActive (true);
        newspaperDeliveryBuy.SetActive (false);
        newspaperDelivery.timeMultiplier = PlayerPrefs.GetFloat ("NewspaperDeliveryTimeMultiplier");
    }

    if (PlayerPrefs.GetInt ("CarWashBought") == 1) { 
        carWash.multiplier = PlayerPrefs.GetFloat ("CarWashMultiplier");
        carWash.upgradeCost = PlayerPrefs.GetFloat ("CarWashUpgradeCost");
        carWash.hired = PlayerPrefs.GetInt ("CarWashHired") == 1 ? true : false;
        if (carWash.hired) {
            carWash.enable ();
            carWash.percentage = PlayerPrefs.GetFloat ("CarWashPercentage");
        }
        carWash.transform.parent.gameObject.SetActive (true);
        carWashBuy.SetActive (false);
        carWash.timeMultiplier = PlayerPrefs.GetFloat ("CarWashTimeMultiplier");
    }

    hasLoaded = true;
}

void askToLoad () {
    modal.choice ("Load previous save?", loadData, dontLoad, null, "Yes", "No", null);
}

void dontLoad () {
    modal.choice ("Not loading will reset your progress, this cannot be undone!", loadData, reset, null, "Load the save", "Reset everything, I'm sure!", null);
}

void reset() {
    PlayerPrefs.DeleteAll ();
    hasLoaded = true;
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ PlayerPrefs really aren't meant for saving the game. Rather for player preferences (settings, for example resolution). Just because you can save the game in PlayerPrefs it doesn't mean you should. Instead use a file for this. \$\endgroup\$
    – zoran404
    Jul 17, 2015 at 17:20

3 Answers 3

1
\$\begingroup\$

Playerprefs are stored in different places depending on if you're in the editor or web player. So information saved in the editor will not be accessible from the web player and vice versa.

\$\endgroup\$
1
  • \$\begingroup\$ That was not the issue I had, since I wasn't trying to transfer saves back and forth between the editor and web player. I found what the issue was. Updating unity seemed to solve it \$\endgroup\$ Jul 17, 2015 at 17:41
0
\$\begingroup\$

Updating Unity to 5.1.2f1 seemed to fix the issue.

\$\endgroup\$
0
\$\begingroup\$

Forget about PlayerPrefs. Serializing your game data will be more secure and more inline with OOP practices. For WebGL builds you'll need to interact with the browser by calling the JavaScript method SyncFiles(), but that can be done completely within your project. I detail the process HERE, including source code and error handling.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .