Grow your YouTube channel like a PRO with a free tool
Get Free YouTube Subscribers, Views and Likes

SAVE u0026 LOAD MULTIPLE OBJECTS In Unity (With tips and debugging at end!)

Follow
Kap Koder

In depth Save and Load Multiple Objects tutorial! with tips and debugging help at the end, saved as binary

Inspired by Brackeys Save System tutorial but optimised to save multiple objects, also a look at what happened in his comment section

Copying code in not just cut and dry

· You need a "SaveSystem" gameObject with the "SaveSystem.cs" on it
· The "SaveSystem" gameObject needs a reference to your objectPrefab
· Your objects script needs an OnDestroy() callback to remove itself from the saves list (optional)
· Your objects script needs an Awake() callback to add itself to the saves list
· If your saving with android then Save with "OnApplicationPause()"

FishData.cs class BELOW

using UnityEngine;

[System.Serializable]
public class FishData
{
//Replace these example variable with your objects variables
//that you wish to save
public int health;
public string name;
public float[] position;

public FishData(Fish fish)
{
health = fish.health;
name = fish.name;

Vector3 fishPos = fish.transform.position;

position = new float[]
{
fishPos.x, fishPos.y, fishPos.z
};
}
}

SaveSystem.cs class BELOW

using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.SceneManagement;

public class SaveSystem : MonoBehaviour
{
[Header("Prefabs")]
[SerializeField] Fish fishPrefab;

//Angled brackets not allowed in desc, replace the
//parenthesis in (Fish) with angled brackets
public static List(Fish) fishes = new List(Fish)();

//Rename your strings according to what your saving
const string FISH_SUB = "/fish";
const string FISH_COUNT_SUB = "/fish.count";

void Awake()
{
LoadFish();
}

//Use if Android
//void OnApplicationPause(bool pause)
//{
// SaveFish();
//}

void OnApplicationQuit()
{
SaveFish();
}

void SaveFish()
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + FISH_SUB + SceneManager.GetActiveScene().buildIndex;
string countPath = Application.persistentDataPath + FISH_COUNT_SUB + SceneManager.GetActiveScene().buildIndex;

FileStream countStream = new FileStream(countPath, FileMode.Create);

formatter.Serialize(countStream, fishes.Count);
countStream.Close();

//Replace "lessThan" with a left angled bracket
for (int i = 0; i lessThan fishes.Count; i++)
{
FileStream stream = new FileStream(path + i, FileMode.Create);
FishData data = new FishData(fishes[i]);

formatter.Serialize(stream, data);
stream.Close();
}
}

void LoadFish()
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + FISH_SUB + SceneManager.GetActiveScene().buildIndex;
string countPath = Application.persistentDataPath + FISH_COUNT_SUB + SceneManager.GetActiveScene().buildIndex;
int fishCount = 0;

if (File.Exists(countPath))
{
FileStream countStream = new FileStream(countPath, FileMode.Open);

fishCount = (int)formatter.Deserialize(countStream);
countStream.Close();
}
else
{
Debug.LogError("Path not found in " + countPath);
}

//Replace "lessThan" with an left angled bracket
for (int i = 0; i lessThan fishCount; i++)
{
if (File.Exists(path + i))
{
FileStream stream = new FileStream(path + i, FileMode.Open);
FishData data = formatter.Deserialize(stream) as FishData;

stream.Close();

Vector3 position = new Vector3(data.position[0], data.position[1], data.position[2]);

Fish fish = Instantiate(fishPrefab, position, Quaternion.identity);

fish.health = data.health;
fish.name = data.name;
}
else
{
Debug.LogError("Path not found in " + (path + i));
}
}
}
}

posted by lappaliehv