- Home /
Saving/Loading gameobject name in .dat file
Hi guys!
I have 3 instantiated gameobjects with names I wish to save to a .dat file, so I can load them back at any point:
gameobject1
gameobject2
gameobject3
All of which has the below script attached.
However, instead of saving all the names of the objects and placing them back, it only saves the last object's name and replaces that name to all the objects that has the script attached.
Here's the code I have so far:
using UnityEngine;
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
public class saveFile : MonoBehaviour {
[System.Serializable]
class playerData
{
public string name;
}
public void Save()
{
BinaryFormatter binary = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath +"/SaveGame.dat");
playerData dat = new playerData();
dat.name = gameObject.transform.name;
binary.Serialize(file, dat);
file.Close();
}
void Awake()
{
if (File.Exists(Application.persistentDataPath + "/SaveGame.dat"))
{
BinaryFormatter binary = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/SaveGame.dat", FileMode.Open);
playerData dat = (playerData)binary.Deserialize(file);
file.Close();
//this sets all the saved data back onto gameobject
name = dat.name;
}
}
}
Answer by JDelekto · Aug 25, 2018 at 09:39 PM
The problem is that each object will be serialized in turn (apparently it is not asynchronous or you would have received errors during serialization).
As each object is serialized in turn, they are going to create a brand new "SaveGame.dat" file, and it will overwrite the file that the previous object had created. The last object to get serialized will then be the "winner" and when de-serialization, all objects will have the last object's name and properties. Recreating the file each time is just overwriting the last serialized object's properties.
You will want to find a different approach to serialize your data. One way to do this would be to put the actual serialization script not onto each object, but perhaps on a parent object or even some other object (like maybe a serialization manager) instead.
You can create a collection of objects which you can add the those you wish to serialize (you could use tags to select them as well), then have your save operation collect all of them and then save them out in one fell swoop.
I tried this, but no luck... nothing is being saved. "SerializationException: serializationStream supports seeking, but its length is 0."
private GameObject[] entry;
void Awake()
{
entry = GameObject.FindGameObjectsWithTag("itemCohort");
}
public void Save()
{
BinaryFormatter binary = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath +"/SaveGame.dat");
playerData dat = new playerData();
for (int i = 0; i < entry.Length; i++) {
dat.name = entry [i].name;
binary.Serialize (file, dat);
}
file.Close ();
}
Do you only have this script on one object?
Yep! On the parent object... Wondering what you thought
Your answer
Follow this Question
Related Questions
"Unbroken Reference" problem when using a custom Editor to Save/Load a ScriptableObject Asset 0 Answers
Saving gameobjects to .NET binary files 1 Answer
Binary Serialization (In Editor) - Path Access denied / File not found 0 Answers
Writing binary files on Android 1 Answer
android serialization weird path issue " IsolatedStorageException ", javascript 0 Answers