I am making a game and I want it so that if the user clicks on New Game it will clear the save file and they can start out from the beginning. How would I do that? I’ll add my save scripts for reference.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameInfo : MonoBehaviour
{
public int Lives = 3;
public int Score = 0;
void Start()
{
LoadPlayer();
}
public void AddPoints()
{
Score += 10;
}
public void LoseLive()
{
Lives -= 1;
}
public void SavePlayer()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer()
{
GameStatus data = SaveSystem.LoadPlayer();
Lives = data.Lives;
Score = data.Score;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GameStatus
{
public int Lives;
public int Score;
public GameStatus(GameInfo player)
{
Lives = player.Lives;
Score = player.Score;
}
}
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SavePlayer(GameInfo player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/game.data";
FileStream stream = new FileStream(path, FileMode.Create);
GameStatus data = new GameStatus(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static GameStatus LoadPlayer()
{
string path = Application.persistentDataPath + "/game.data";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
GameStatus data = formatter.Deserialize(stream) as GameStatus;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
}