Hello there, hope this is the right place to ask or debate this.
I’ve been developing this game for android with the idea of creating at least 30 levels, each level with X amount of coins to collect.
Today I watched the live training in Persistence, Saving and Loading which helped me a lot, but what I want now is to save the current state of a level, for example, a player picks up 2 out of 5 coins in this level and continues to the next one (saving the current level). Then the next time the player comes back to that level, those 2 coins continue inactive and the rest still there to pick up.
I’ve done this so far:
public void Save () {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData ();
data.totalCoins = totalCoins;
bf.Serialize (file, data);
file.Close ();
Debug.Log ("Saved!");
}
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();
totalCoins = data.totalCoins;
Debug.Log ("Loaded!");
}
}
}
[Serializable]
public class Level {
public string name;
public int levelIndex;
public string achievementID;
public int coinsInLevel;
public GameObject[] coins;
}
[Serializable]
class PlayerData {
public int totalCoins;
public Level level;
}
So basically I have GameObject[ ] coins to control if the coin has been picked up or not (with coins[ ].activeSelf) and all that stuff.
The problem is, I can’t save a class into a file nor can I save the array.
What should I do?