[Serialization] Multiple classes into one file?

I’m new to binary serialization and have already encountered a problem that’s left me perplexed.

What I want to do is this: Take data from two custom classes and serialize it into a single “save” file.

It sounds simple, but I can’t for the life of me figure out how to go serializing them simultaneously via “bf.Serialize()” (where “bf” is the name of the Binary Formatter).

Just so you guys have an idea of what I’m working with, here’s my code:

public class SaveLoad : MonoBehaviour {

    GameObject player;
    GameObject[] enemies;

    public void SaveGame(string fileName)
        // Grab player & enemies
        player = GameObject.FindGameObjectWithTag("Player");
        enemies = GameObject.FindGameObjectsWithTag("Enemy");

        // Prepare to write
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/" + fileName);

        // Get player data
        PlayerData playerData = new PlayerData();

        playerData.xPos = player.transform.position.x;
        playerData.yPos = player.transform.position.y;
        playerData.health = player.GetComponent<PlayerStats>().health;
        playerData.hasKey = player.GetComponent<PlayerStats>().hasKey;

        // Get enemy data
        EnemyData[] enemyData = new EnemyData[enemies.Length];
        for (int i = 0; i < enemyData.Length; i++) enemyData *= new EnemyData();  // Initialize class instances*

for (int i = 0; i < enemies.Length; i++)
enemyData_.instanceID = enemies*.GetInstanceID();
enemyData.xPos = enemies.transform.position.x;
enemyData.yPos = enemies.transform.position.y;
enemyData.health = enemies.GetComponent().health;

// Write data to disk
bf.Serialize(file, playerData); // PROBLEM HERE

class PlayerData
public float xPos;
public float yPos;
public int health;
public bool hasKey;

class EnemyData
public float instanceID;
public float xPos;
public float yPos;
public int health;
The “PROBLEM HERE” comment shows where I’m stuck. I can plug in either playerData or enemyData, but not both. As a result, I can only serialize one of the classes! How the heck would I go about serializing both of them?
I’d also like to point out two things:
- The PlayerData and EnemyData classes
are extremely similar only because
this is a small test project. If I
were making an actual game, they’d
obviously have far more differences.
As a result, these classes must be
kept separately. I don’t want to
combine them into a single unified
“data” class.
- I realize I could split
the classes up into two separate
binary files, and just load (deserialize) them
one-after-another, but I don’t want
to do that. I want to have a single
save file that stores all of my data.
Thanks in advance for any help!


I think something like that will solve your problem.

class AllData{
    public PlayerData playerData;
    public EnemyData[] enemyDatas;


Serialize this class instead of serializing them separately. I do it all the time and i don’t see any downside of this.