Question by
dasani2406 · Apr 15, 2017 at 12:02 PM ·
serializationsaveloadsave data
How do i add coins in my Save Total Coin, everytime i collect more coins in the game?
I have a Save "Total Coin" and I want the add coins in the Save "Total Coin" every time I collect some in the game. My Save and load scripts, right here:
public class ScoreManager : MonoBehaviour {
public int CoinCount;
public void Save()
{
BinaryFormatter BF = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/PlayerInfo.dat");
PlayerData data = new PlayerData();
data.coinCount = CoinCount;
data.Highscore = HiScoreCount;
BF.Serialize(file, data);
file.Close();
}
public void load()
{
if (File.Exists(Application.persistentDataPath + "/PlayerInfo.dat"))
{
BinaryFormatter BF = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/PLayerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)BF.Deserialize(file);
file.Close();
CoinCount = data.coinCount;
HiScoreCount = data.Highscore;
}
}
}
[Serializable]
class PlayerData
{
public int coinCount;
public float Highscore;
}
I collect the coins with this script:
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("coins"))
{
ScoreManager.GetInstance().CoinCount += 1;
other.gameObject.SetActive(false);
}
}
I am stuck and cannot find any solutions. Some help please.
Comment