- Home /
How to save a list of MonoBehaviours in a binary file
I have a code that saves data in the data file are saved without any problem.But I also tried to save an array but the array was not saved and I get an error.I keep the data (and also the array) in BinaryFormatter.The error indicates the line bf.Serialize (file, data) before closing the file in the save function.
the code
public class SaveLoadScript : MonoBehaviour
{
public int allMoneyOfplayer, allXpOfPlayer, laval, maxXp, howMuchUpMaxXp;
public float highTime;
public List<GunScript> gun = new List<GunScript>();
public void Start()
{
Save();
}
public void Update()
{
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/saveG.file");
PlayerData data = new PlayerData();
data.money = allMoneyOfplayer;
data.xp = allXpOfPlayer;
if (highTime > data.time)
data.time = highTime;
data.laval = laval;
data.maxXp = maxXp;
data.howMuchUpMaxXp = howMuchUpMaxXp;
for (int i = 0; i < gun.Count; i++)
data.gun.Add(gun[i]);
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/saveG.file"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/saveG.file", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
allMoneyOfplayer = data.money;
allXpOfPlayer = data.xp;
highTime = data.time;
laval = data.laval;
maxXp = data.maxXp;
howMuchUpMaxXp = data.howMuchUpMaxXp;
file.Close();
}
}
}
[System.Serializable]
class PlayerData
{
public int money;
public int xp;
public float time;
public int laval;
public int maxXp;
public int howMuchUpMaxXp;
public List<GunScript> gun = new List<GunScript>();
}
the error
SerializationException: Type 'GunScript' in Assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.FormatterServices+<>c__DisplayClass9_0.<GetSerializableMembers>b__0 (System.Runtime.Serialization.MemberHolder _) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, System.Runtime.Serialization.StreamingContext context) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo () (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize (System.Type objectType, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.SerializationBinder binder) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize (System.Type objectType, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.SerializationBinder binder) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo memberObjectInfo) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo typeNameInfo) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize (System.Object graph, System.Runtime.Remoting.Messaging.Header[] inHeaders, System.Runtime.Serialization.Formatters.Binary.__BinaryWriter serWriter, System.Boolean fCheck) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers, System.Boolean fCheck) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at <fb001e01371b4adca20013e0ac763896>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at <fb001e01371b4adca20013e0ac763896>:0)
Thanks for the help
Answer is hidden in plain sight:
(...) GunScript (...) is not marked as serializable.
I changed the GunScript class and added a GunScriptData class I added to the [System.Serializable] class and still get the same error
public class GunScript : MonoBehaviour
{
Vector2 mousePosition;
Vector2 directionMouse;
float spasBetweenBuiltToBuilt;
public GameObject built;
public Transform honestOfGun;
public Rigidbody2D rb;
public Rigidbody player;
// Start is called before the first frame update
void Start()
{
spasBetweenBuiltToBuilt = 0f;
}
// Update is called once per frame
void Update()
{
FollowAfterMouse();
Shoot();
}
void Shoot()
{
spasBetweenBuiltToBuilt += 1 * Time.deltaTime;
if (Input.GetMouseButton(0) && spasBetweenBuiltToBuilt >= 0.2)
{
Instantiate(built, honestOfGun.position, honestOfGun.rotation);
spasBetweenBuiltToBuilt = 0f;
}
}
void FollowAfterMouse()
{
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
directionMouse = new Vector2(mousePosition.x - rb.position.x, mousePosition.y - rb.position.y);
float angle = Mathf.Atan2(directionMouse.x, directionMouse.y) * Mathf.Rad2Deg - 90f;
rb.transform.rotation = Quaternion.Euler(0, 0, -angle);
rb.position = player.position;
}
}
[System.Serializable]
public class GunScriptData
{
public GameObject GM;
public bool bayYesOrNo = false;
public int gunAttack, sill;
}
Don't serialize GameObject
s and/or their Monobehaviour
components directly. Serialize their states in some kind of predefined data type. GunScriptData
is a good general direction but it contains a GameObject
field which will spell this kind of trouble when serializing. Replace this field with different data type.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Serialization in unity Iphone basic 3 Answers
Saving a list from a class containing gamobjects 0 Answers
Storing different scripts into a list iterating issue 2 Answers
Strategy for saving game 1 Answer