So in my code I’m using playerprefs to save and load data between scenes, but I want all of the data to be erased when I close out of the game. For this I’m using OnApplicationQuit, which through Debog.Log I’ve proved gets called when I close the game.
public void OnDisable()
{
PlayerPrefs.SetInt("canUseSpell", canUseSpell);
PlayerPrefs.SetInt("spd1", spd1SetActive);
PlayerPrefs.SetInt("spd2", spd2SetActive);
PlayerPrefs.SetInt("spd3", spd3SetActive);
PlayerPrefs.SetInt("maxHP1", MaxHp1SetActive);
PlayerPrefs.SetInt("maxHP2", MaxHp2SetActive);
PlayerPrefs.SetInt("maxMP1", MaxMp1SetActive);
PlayerPrefs.SetInt("maxMP2", MaxMp2SetActive);
PlayerPrefs.SetInt("HpRegen1", HPRegen_1SetActive);
PlayerPrefs.SetInt("HpRegen2", HPRegen_2SetActive);
PlayerPrefs.SetInt("MpRegen1", MPRegen_1SetActive);
PlayerPrefs.SetInt("MpRegen2", MPRegen_2SetActive);
PlayerPrefs.SetInt("Dex1", Dexterity_1SetActive);
PlayerPrefs.SetInt("Dex2", Dexterity_2SetActive);
PlayerPrefs.SetInt("Def1", Defense_1SetActive);
PlayerPrefs.SetInt("Def2", Defense_2SetActive);
PlayerPrefs.SetInt("Def3", Defense_3SetActive);
PlayerPrefs.SetFloat("moveSpeed", moveSpeed);
PlayerPrefs.SetInt("HPperSec", HPperSec);
PlayerPrefs.SetInt("MPperSec", MPperSec);
PlayerPrefs.SetFloat("Dex", fireSpeed);
PlayerPrefs.SetInt("Def", Defense);
PlayerPrefs.SetInt("level", level);
PlayerPrefs.SetFloat("HP", Hitpoints);
PlayerPrefs.SetFloat("MaxHP", MaxHitpoints);
PlayerPrefs.SetFloat("MP", Mana);
PlayerPrefs.SetFloat("MaxMP", MaxMana);
PlayerPrefs.SetInt("upgradePoints", upgradePoints);
PlayerPrefs.SetInt("xp", experience);
PlayerPrefs.SetInt("xpToNextLvl", experienceToNextLevel);
}
public void OnEnable()
{
canUseSpell = PlayerPrefs.GetInt("canUseSpell", 0);
spd1SetActive = PlayerPrefs.GetInt("spd1", 0);
spd2SetActive = PlayerPrefs.GetInt("spd2", 0);
spd3SetActive = PlayerPrefs.GetInt("spd3", 0);
MaxHp1SetActive = PlayerPrefs.GetInt("maxHP1", 0);
MaxHp2SetActive = PlayerPrefs.GetInt("maxHP2", 0);
MaxMp1SetActive = PlayerPrefs.GetInt("maxMP1", 0);
MaxMp2SetActive = PlayerPrefs.GetInt("maxMP2", 0);
HPRegen_1SetActive = PlayerPrefs.GetInt("HpRegen1", 0);
HPRegen_2SetActive = PlayerPrefs.GetInt("HpRegen2", 0);
MPRegen_1SetActive = PlayerPrefs.GetInt("MpRegen1", 0);
MPRegen_2SetActive = PlayerPrefs.GetInt("MpRegen2", 0);
Dexterity_1SetActive = PlayerPrefs.GetInt("Dex1", 0);
Dexterity_2SetActive = PlayerPrefs.GetInt("Dex2", 0);
Defense_1SetActive = PlayerPrefs.GetInt("Def1", 0);
Defense_2SetActive = PlayerPrefs.GetInt("Def2", 0);
moveSpeed = PlayerPrefs.GetFloat("moveSpeed", 1);
HPperSec = PlayerPrefs.GetInt("HPperSec", 1);
MPperSec = PlayerPrefs.GetInt("MPperSec", 1);
fireSpeed = PlayerPrefs.GetFloat("Dex", 0.3f);
Defense = PlayerPrefs.GetInt("Def", 0);
level = PlayerPrefs.GetInt("level", 0);
Hitpoints = PlayerPrefs.GetFloat("HP", 800);
MaxHitpoints = PlayerPrefs.GetFloat("MaxHP", 800);
Mana = PlayerPrefs.GetFloat("MP", 50);
MaxMana = PlayerPrefs.GetFloat("MaxMP", 50);
upgradePoints = PlayerPrefs.GetInt("upgradePoints", 0);
experience = PlayerPrefs.GetInt("xp", 0);
experienceToNextLevel = PlayerPrefs.GetInt("xpToNextLvl", 30);
playerSkills.SetSkillPoint(upgradePoints);
}
public void OnApplicationQuit()
{
PlayerPrefs.DeleteAll();
}
I know this code is pretty ugly but hey, it works. However when OnApplicationQuit gets called, the values for all of my PlayerPrefs don’t change back to their default values I have set. Any help or different ways to save this data would be greatly appreciated!