Question by
MidievalMan · Aug 15, 2020 at 07:33 AM ·
savesave datareset
How to Reset Save Data
Hello, I know little about saving data and my game is very simple, so I followed Brackeys tutorial on saving and loading in Unity, unfortunately, the tutorial did not cover how to reset save data and I'm stuck. I tried a thing or two, but my reset function just throws the Debug.LogError. If you've got any ideas on how to do this, I'd greatly appreciate it! Here's all my code:
class for data:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData
{
public float turretMaxHealth = 100;
public float turretCurrentHealth = 1;
public float turretDamage = 10;
public float currentMoney = 0;
public int currentLevel = 0;
public float currentExperience = 0f;
public float nextFire = 50;
public float criticalHit = 1;
public float healthUpgradeCost = 100f;
public float damageUpgradeCost = 100f;
public float fireRateUpgradeCost = 100f;
public float criticalHitUpgradeCost = 100f;
public PlayerData (GameManagerScript player)
{
turretMaxHealth = player.turretMaxHealth;
turretCurrentHealth = player.turretCurrentHealth;
turretDamage = player.turretDamage;
currentMoney = player.currentMoney;
currentLevel = player.currentLevel;
currentExperience = player.currentExperience;
nextFire = player.nextFire;
criticalHit = player.criticalHit;
healthUpgradeCost = player.healthUpgradeCost;
damageUpgradeCost = player.damageUpgradeCost;
fireRateUpgradeCost = player.fireRateUpgradeCost;
criticalHitUpgradeCost = player.criticalHitUpgradeCost;
}
}
Part of the script that calls the functions:
public void SavePlayer()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer()
{
PlayerData data = SaveSystem.LoadPlayer();
turretMaxHealth = data.turretMaxHealth;
turretCurrentHealth = data.turretCurrentHealth;
turretDamage = data.turretDamage;
currentMoney = data.currentMoney;
currentLevel = data.currentLevel;
currentExperience = data.currentExperience;
nextFire = data.nextFire;
criticalHit = data.criticalHit;
healthUpgradeCost = data.healthUpgradeCost;
damageUpgradeCost = data.damageUpgradeCost;
fireRateUpgradeCost = data.fireRateUpgradeCost;
criticalHitUpgradeCost = data.criticalHitUpgradeCost;
healthBar.SetMaxHealth(turretMaxHealth);
}
public void ResetPlayer()
{
SaveSystem.ResetPlayer();
}
Class for saving and loading data:
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SavePlayer(GameManagerScript player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.save";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData LoadPlayer()
{
string path = Application.persistentDataPath + "/player.save";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
public static void ResetPlayer()
{
string path = Application.persistentDataPath + "/player.save";
if (File.Exists(path))
{
File.Delete(path);
}
else
{
Debug.LogError("Save file not found in " + path);
}
}
}
Thanks again!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613005711im_/https://answers.unity.com/themes/thub/images/avi.jpg)