0

Well i am making a simple PlayerPrefs Script that should load my stats and save them every frame i attached it on a "DontDestroyOnLoad" Object that is in every scene but for some reason on starting the game all stats become 0 here as my code says im saving the stats every 10 seconds as i read in solutions PlayerPrefs.save should fix it but it doesnt

void Start () {
    InvokeRepeating ("SaveGame",0,10f);
    Stats.totalcoins = PlayerPrefs.GetInt ("totalcoins");
    Stats.personalbest = PlayerPrefs.GetInt ("PersonalBest");
    Stats.CarrotSpawnRateLVL = PlayerPrefs.GetInt ("CarrotSpawnRateLvL");
    Stats.CarrotSpawnRate = PlayerPrefs.GetFloat ("CarrotSpawnRate");
    Stats.CarrotSpawnRateUpgradeCost = PlayerPrefs.GetInt ("CarrotSpawnRateUpgradeCost");
    Stats.CarrotEffectLVL = PlayerPrefs.GetInt ("CarrotEffectLvL");
    Stats.CarrotEffect = PlayerPrefs.GetFloat ("CarrotEffect");
    Stats.CarrotEffectUpgradeCost = PlayerPrefs.GetInt ("CarrotEffectUpgradeCost");
    Stats.UmbrellaDurabilityLVL = PlayerPrefs.GetInt ("UmbrellaDurabilityLvL");
    Stats.UmbrellaDurability = PlayerPrefs.GetFloat ("UmbrellaDurability");
    Stats.UmbrellaDurabilityUpgradeCost = PlayerPrefs.GetInt ("UmbrellaDurabilityUpgradeCost");
    Stats.UmbrellaSizeLVL = PlayerPrefs.GetInt ("UmbrellaSizeLvL");
    Stats.UmbrellaSize = PlayerPrefs.GetFloat ("UmbrellaSize");
    Stats.UmbrellaSizeUpgradeCost = PlayerPrefs.GetInt ("UmbrellaSizeUpgadeCost");
    Stats.Tree1Bought = PlayerPrefs.GetInt ("Tree1Bought");
    Stats.Tree1SizeLVL = PlayerPrefs.GetInt ("Tree1LvL");
    Stats.Tree1Size = PlayerPrefs.GetFloat ("Tree1Size");
    Stats.Tree1SizeUpgradeCost = PlayerPrefs.GetInt ("Tree1UpgradeCost");
    Stats.Tree2Bought = PlayerPrefs.GetInt ("Tree2Bought");
    Stats.Tree2SizeLVL = PlayerPrefs.GetInt ("Tree2LvL");
    Stats.Tree2Size = PlayerPrefs.GetFloat ("Tree2Size");
    Stats.Tree2SizeUpgradeCost = PlayerPrefs.GetInt ("Tree2UpgradeCost");
    Stats.Tree3Bought = PlayerPrefs.GetInt ("Tree3Bought");
    Stats.Tree3SizeLVL = PlayerPrefs.GetInt ("Tree3LvL");
    Stats.Tree3Size = PlayerPrefs.GetFloat ("Tree3Size");
    Stats.Tree3SizeUpgradeCost = PlayerPrefs.GetInt ("Tree3UpgradeCost");
    Stats.BuyTreesCost = PlayerPrefs.GetInt ("BuyTreeCost");

}

// Update is called once per frame
void Update () {
    Debug.Log (Stats.totalcoins);
    PlayerPrefs.SetInt ("totalcoins", Stats.totalcoins);
    PlayerPrefs.SetInt ("PersonalBest", Stats.personalbest);
    PlayerPrefs.SetInt ("CarrotSpawnRateLvL", Stats.CarrotSpawnRateLVL);
    PlayerPrefs.SetFloat ("CarrotSpawnRate", Stats.CarrotSpawnRate);
    PlayerPrefs.SetInt ("CarrotSpawnRateUpgradeCost", Stats.CarrotSpawnRateUpgradeCost);
    PlayerPrefs.SetInt ("CarrotEffectLvL", Stats.CarrotEffectLVL);
    PlayerPrefs.SetFloat ("CarrotEffect", Stats.CarrotEffect);
    PlayerPrefs.SetInt ("CarrotEffectUpgradeCost", Stats.CarrotEffectUpgradeCost);
    PlayerPrefs.SetInt ("UmbrellaDurabilityLvL", Stats.UmbrellaDurabilityLVL);
    PlayerPrefs.SetFloat ("UmbrellaDurability", Stats.UmbrellaDurability);
    PlayerPrefs.SetInt ("UmbrellaDurabilityUpgradeCost", Stats.UmbrellaDurabilityUpgradeCost);
    PlayerPrefs.SetInt ("UmbrellaSizeLvL", Stats.UmbrellaSizeLVL);
    PlayerPrefs.SetFloat ("UmbrellaSize", Stats.UmbrellaSize);
    PlayerPrefs.SetInt ("UmbrellaSizeUpgadeCost", Stats.UmbrellaSizeUpgradeCost);
    PlayerPrefs.SetInt ("Tree1Bought", Stats.Tree1Bought);
    PlayerPrefs.SetInt ("Tree1LvL", Stats.Tree1SizeLVL);
    PlayerPrefs.SetFloat ("Tree1Size", Stats.Tree1Size);
    PlayerPrefs.SetInt ("Tree1UpgradeCost", Stats.Tree1SizeUpgradeCost);
    PlayerPrefs.SetInt ("Tree2Bought", Stats.Tree2Bought);
    PlayerPrefs.SetInt ("Tree2LvL", Stats.Tree2SizeLVL);
    PlayerPrefs.SetFloat ("Tree2Size", Stats.Tree2Size);
    PlayerPrefs.SetInt ("Tree2UpgradeCost", Stats.Tree2SizeUpgradeCost);
    PlayerPrefs.SetInt ("Tree3Bought", Stats.Tree3Bought);
    PlayerPrefs.SetInt ("Tree3LvL", Stats.Tree3SizeLVL);
    PlayerPrefs.SetFloat ("Tree3Size", Stats.Tree3Size);
    PlayerPrefs.SetInt ("Tree3UpgradeCost", Stats.Tree3SizeUpgradeCost);
    PlayerPrefs.SetInt ("BuyTreeCost", Stats.BuyTreesCost);*/

}
void SaveGame (){
    PlayerPrefs.Save ();
}
Nikolay Atanasov
  • 215
  • 2
  • 12
  • Consider using a better way of saving data other than PlayerPrefs. PlayerPrefs are awfully painful to load and save and sometimes bring bugs with them. Here is an [example](https://gamedevelopment.tutsplus.com/tutorials/how-to-save-and-load-your-players-progress-in-unity--cms-20934) on data serialization. – Ian H. Dec 07 '17 at 11:19
  • My eyes are bleeding by the abuse of `PlayerPrefs`. If you have to save multiple data, use another method. See duplicate. Please do **not** save data every frame. You are unnecessary slowing down your game and slowly destroying your HDD. – Programmer Dec 07 '17 at 11:21

0 Answers0