Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Aubrey Hesselgren · Jul 18, 2011 at 02:51 PM · prefabeditor-scriptingsavelists

List or Array of Custom Classes not saving into prefabs properly?

I have a class defined inside a component. It's more or less just a struct of raw data:, i.e.

 [ExecuteInEditMode]
 class ListOfData : MonoBehavior 
 {
     public class DataElement
     {
         public float stuff;
         public GameObject thing;
         //etc.
     }
 
     List<DataElement> mListOfDatas;
 
 
 
     void Update()
     {
     
 
 
         if (!Application.isPlaying)
         {
              mListOfDatas = mListOfDatas ?? new List<DataElement>();
         }
     }
 
 }


I also have an editor script, changing the contents of mListOfDatas. Unfortunately, this data is not showing up in the component UI, and i'm guessing that only stuff shown in the UI will actually save. Is this the case, and is there a way to force this data to save to the prefab, or force it to display?

I think I'm getting to the limits of the prefab system, here.

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
2
Best Answer

Answer by Bunny83 · Jul 18, 2011 at 02:58 PM

First, only public variables or private variables with SerializeField attribute are serialized. Also you can only serialize arrays or lists of serializeable types. You can turn your DataElement class into a serialized type by adding the Serializable attribute

 class ListOfData : MonoBehavior 
 {
     [System.Serializable]
     public class DataElement
     {
         public float stuff;
         public GameObject thing;
         //etc.
     }
     [SerializeField]
     List<DataElement> mListOfDatas;

Comment
Add comment · Show 4 · 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 Aubrey Hesselgren · Jul 18, 2011 at 03:15 PM 0
Share

Thanks so much! Worked perfectly!

avatar image Aubrey Hesselgren · Jul 20, 2011 at 10:36 AM 0
Share

I lied! It only seems to store the first element when I close and re-open. And sometimes forgets normal, "public" stuff. Does using [SerializeField] once mean I have to start using it everywhere in that class?

avatar image kovata · Nov 01, 2015 at 08:27 AM 0
Share

I have a question about this code. How to serialize list of class or struct from another script?

avatar image Bunny83 kovata · Nov 01, 2015 at 11:34 AM 0
Share

This question was about unity's serialization system which can only serialize things inside the editor. If you have a different question, please ask a question and don't wake a 4+ years old thread. Also your one-liner you've posted is barely enough to actually understand what you want to do.

Ask a seperate question. You can include a link to this question / answer for reference, but make sure you describe your question in a way it can be understood. Example code would help, even when it's pseudocode.

avatar image
1

Answer by Molix · Jul 18, 2011 at 02:59 PM

Add [Serializable] before your custom class declaration. Then the public members will be saved/editable like the ones in your MonoBehaviour.

i.e.

 [Serializable]
 public class DataElement
     {
 ...
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to modify prefab permanently via script. 0 Answers

How to provide undo for EditorScript changes to a prefab? 1 Answer

Best method to save states in/for a prefab 0 Answers

How to save a prefab when a button is pressed 1 Answer

How do I programmatically assign a GameObject to a prefab? 6 Answers


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