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
0
Question by gyrosp · Sep 29, 2015 at 10:53 AM · serializationmonobehaviourserializablecustom class

Custom class does not get serialized

Hello.

I have a class Cookbookmanager which ineherits from MonoBehaviour. The Cookbookmanager has a class member CustomRecipe from a custom class which is marked as serializable.

Unfortunately this member does no get serialized, its values are always empty.

Cookbookmanager:

 public class CookbookManager: MonoBehaviour 
 {
     private static int _maxRecipes = 16;
 
     [SerializeField]
     public CustomRecipe _TEST_RECIPE;

 void Start () 
 {
     //_TEST_RECIPE = new CustomRecipe();
     //_TEST_RECIPE.Name = "some name";
     Debug.Log("Test-Recipe-Name: " + _TEST_RECIPE.Name);
 }

 }

CustomRecipe:

     [System.Serializable]
     public class CustomRecipe
     {
         private const int _maxIngredients = 6;
     
         [SerializeField]
         private string         _name;
         [SerializeField]
         private string         _description;
 
     public string Name
     {
         set { _name = value; }
         get { return _name; }
     }
     }

I started the progamm without the comments of course and the Debug.Log showed the correct recipe name as expected. After that I outcommented the lines where I set the recipe but as it seemed the recipe was not serialized.

Any idea what is going wrong here?


Edit: The CookbookManager has a partent GameObject which is a singleton and which uses DontDestroyOnLoad(). Can this cause the trouble with the serialization?

Comment
Add comment · Show 2
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 RChrispy · Sep 29, 2015 at 10:58 AM 0
Share

I am not realy sure but those are private fields in CustomRecipe and if you dont safe the values you changed in the inspector in the prefab then it will always start with a null value!

Do you want to have a standart value, that is also displayed in the editor am i right?

avatar image gyrosp RChrispy · Sep 29, 2015 at 11:01 AM 0
Share

The private fields are marked as SerializeField and therefor should be serialized. But I also tried using public fields, it did not work either.

I'm not using the inspector. Values are only altered via script.

2 Replies

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

Answer by Xarbrough · Sep 29, 2015 at 05:36 PM

Reading your comments, you are "not using the inspector"? So how are you saving your data then? Scriptable Object or Prefab? If not, where should the serialized data be saved to? Also, you actually need to create an object and assign your data, before you can save anything. So either you need to do that via the inspector, or via script and then save to an asset file.

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 gyrosp · Sep 29, 2015 at 07:03 PM 2
Share

You are right, too much program$$anonymous$$g today. I put my data in a ScriptableObject and now its working as expected. Thanks

avatar image Kyle_WG · Sep 30, 2015 at 09:36 AM 0
Share

Ha, just assumed that @gyrosp was using inspector here with an object in the scene active. Looking back at the edits I can see this now. Excellent.

avatar image
0

Answer by Kyle_WG · Sep 29, 2015 at 12:42 PM

That should technically work alright!? Weird. Try separating those into 2 files and recompiling.

Also try (long shot):

 public CustomRecipe _TEST_RECIPE = new CustomRecipe();

Just so it creates a fresh reference straight away.

Check for duplicates too.

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 gyrosp · Sep 29, 2015 at 02:15 PM 0
Share
  public CustomRecipe _TEST_RECIPE = new CustomRecipe();

unfortunately does not change anything. Both classes are already in two separate files. Any other idea :)?

avatar image Kyle_WG gyrosp · Sep 29, 2015 at 03:40 PM 0
Share

Ack. Try a fresh instance of Unity with just these classes in. Also try removing static for now.

avatar image Bunny83 · Sep 30, 2015 at 10:46 AM 1
Share

You don't have to / shouldn't create serialized custom classes manually. Those are created automatically by the Inspector.

The code in the OP won't work since he created an instance manually at runtime. What you do at runtime isn't reflected by the assets as Unity doesn't serialize anything at runtime.

So either create them "manually" from an editor script at edit time, or simply use the inspector to setup your object data.

avatar image Kyle_WG Bunny83 · Sep 30, 2015 at 10:50 AM 0
Share

Yeah, @Xarbrough pointed out in the marked answer that he was doing it runtime. I overlooked that before @gyrosp's edits as assumed he was trying to see the changes in the inspector from a GameObject in the Hierarchy haha. :D

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

32 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

Related Questions

Instantiating a serializable class and variable 1 Answer

Serialization\Deserialization problem 2 Answers

Serialized data show information after load 0 Answers

UnityEngine.GameObject not marked as Serializable? 1 Answer

Help with editor serialization 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