- Home /
I have watched Brackeys Saving system video but its not working
This is for an android game. // General is the name of my script //
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData
{
public int red = 0;
public int yellow = 0;
public int green = 0;
public int purple = 0;
public int black = 0;
public int white = 0;
public int cyan = 0;
public int pink = 0;
public int orange = 0;
public int Money;
public int stars = 0;
public int Skin01 = 0;
public int Skin02 = 0;
public int Skin03 = 0;
public int Skin04 = 0;
public int Skin05 = 0;
public PlayerData (General player)
{
red = player.red;
yellow = player.yellow;
green = player.green;
purple = player.purple;
black = player.black;
white = player.white;
cyan = player.cyan;
pink = player.pink;
orange = player.orange;
Money = player.Money;
stars = player.stars;
Skin01 = player.Skin01;
Skin02 = player.Skin02;
Skin03 = player.Skin03;
Skin04 = player.Skin04;
Skin05 = player.Skin05;
}
}
second script:
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
public static class SaveSystem
{
public static void SavePlayer (General player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/GameSaveData.beans";
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 + "/GameSaveData.beans";
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;
}
}
}
my script:
...
...
private void Awake()
{
LoadPlayer();
}
...
public void SavePlayerFunc()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer()
{
PlayerData data = SaveSystem.LoadPlayer();
red = data.red;
yellow = data.yellow;
green = data.green;
purple = data.purple;
black = data.black;
white = data.white;
cyan = data.cyan;
pink = data.pink;
orange = data.orange;
Skin01 = data.Skin01;
Skin02 = data.Skin02;
Skin03 = data.Skin03;
Skin04 = data.Skin04;
Skin05 = data.Skin05;
}
...
public void Continue()
{
...//called by a button
SavePlayerFunc();
}
...
Temporarily replace BinaryFormatter
with JsonUtility
. This will allow you to read saved file and inspect what exactly is being saved etc.
Answer by PronKill · Jul 07, 2021 at 01:44 PM
Just use PlayerPrefs. They work good. https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
player prefs can be bulky for big projects...im assu$$anonymous$$g that's why he followed the tutorial anyway because otherwise playerprefs are the first things to pop up when you google this stuff
When I was searching for saving/loading, I've seen PlayerPrefs only after hours of searching because BinaryFormatter wasn't working like I wanted to.
strange, i found like 100s of player prefs articles and some json solutions before landing on a binary formatter one