Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by cyangamer 1 · Jul 30, 2010 at 09:20 AM · errormonobehaviourexceptionserialize

Need to serialize a Monobehaviour Class

I need to serialize a class that inherits from Monobehaviour in order to save my game data, but I'm getting an error:

SerializationException: Type UnityEngine.MonoBehaviour in assembly UnityEngine, Version=0.0.0.0, Culture=neutral is not marked as serializable.

I heard that Monobehaviour was serializable (that would be a glaring limitation in the engine if it wasn't). I've marked all my custom classes as serializable, but is there another command I need in order to make Monobehaviour serializable?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Mike 3 · Jul 30, 2010 at 09:36 AM

MonoBehaviour isn't serializable no - but that's probably a good thing. You'd be able to serialize it, but not deserialize. You can't just do new MonoBehaviourDerivative() to create a monobehaviour, it's got to be done with AddComponent, meaning you'd need completely different deserialization for it anyway

What I would do is use the Memento pattern - an object you have an instance of from your script which is serializable itself, which keeps all the data for the object

Makes it rather easy to serialize and deserialize your data.

Alternatively, there is a third party addon called EZGUI, which serializes monobehaviours by their public properties and fields

Comment
Add comment · Show 2 · Share
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 cyangamer 1 · Jul 30, 2010 at 11:52 AM 0
Share

Thanks, I'll try the $$anonymous$$emento pattern...

avatar image SrBilyon · Jan 11, 2012 at 04:04 PM 1
Share

Could this be elaborated upon? I'm trying the same thing now

avatar image
2

Answer by MisterBinny · Aug 26, 2016 at 04:47 AM

For everyone else hopelessly confused... (this works as of 8/25/2016.)

Step #1. Create an Object that is to be serializable; Add the following to the top of your code

using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary;

Step #2. next add [Serialisable] just above the "Public class myObject"

Step #3. Remove "MonoBehavior", so it doesn't inherit from that

Step #4. Instance your object from another class that inherits from MonoBehavior, and place that script and object in the heirarchy somewhere of your project like you would any other.

MSDN has more information https://msdn.microsoft.com/en-us/library/ms973893.aspx

Step #5. Also... check your work. When the file is written you should inspect it with a hex editor, it will write to a file in your root directory unless you specified otherwise.

If you have questions let me know.

Comment
Add comment · Show 2 · Share
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 jj_unity328 · Jul 23, 2018 at 12:30 PM 0
Share

Just use a ScriptableObject if you want to do that.

avatar image dpoly jj_unity328 · May 23, 2019 at 05:44 AM 0
Share

Spot on. Forget the rest, just use ScriptableObject.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Package directory not found for DataContractSerialization 1 Answer

Help with error: UnassignedReferenceException 5 Answers

can someone help me with this error please? 1 Answer

Referencing NON-monobehavior scripts - getting an error :/ 1 Answer

Error "m_CoroutineEnumeratorGCHandle == 0"? 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