Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yair710 · Nov 22, 2021 at 04:50 PM · listsavebinaryformatter

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

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Nov 22, 2021 at 10:33 AM 0
Share

Answer is hidden in plain sight:

(...) GunScript (...) is not marked as serializable.

avatar image yair710 andrew-lukasik · Nov 22, 2021 at 11:41 AM 0
Share

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;
 }
avatar image andrew-lukasik yair710 · Nov 22, 2021 at 12:38 PM 0
Share

Don't serialize GameObjects 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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges