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
3
Question by CorruptedTNC · Feb 05, 2017 at 02:37 PM · c#serializationimplementationaccessor

Why does Serialization not work in Unity with auto-implemented properties?

So I've just spent several hours wondering why one of my database assets won't save. After reading as many resources as I could find on the subject, none of the solutions worked. These included SetDirty and almost every other method belonging to the AssetDatabase class.

However, my problem was that manually implemented properties are apparently what I'm meant to be using:

 using System;
 
 [Serializable]
 public class Item
 {
     [SerializeField] private int _id;
     
     //Constructors etc
     
     public int Id
     {
         get { return _id; }
         set { _id = value; }
     }
 }

Whereas auto-implemented properties, which are far less prone to manual entry errors and are simply less taxing to read and write do not:

 using System;
 
 [Serializable]
 public class Item
 {
     //Constructors etc

     [SerializeField] public int Id { get; set;}
 }

Is this a C# language problem that I'm not aware of? Or is this something that Unity does specifically? I'm slightly put off by the idea of using .asset files now if it means typing out 400% more boiler plate.

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 Bunny83 · Feb 06, 2017 at 05:22 AM 0
Share

You might look into some limitations about Unity's serialization system. Before you dive too deep into creating an item system you should be aware that Unity's serialization system does not support inheritance for custom serializable classes.

I strongly suggest you read the docs on serialization, or just the blog post which essentially lead to that doc page. The blog is slightly more readable since it has better syntax highlighting and better contrast

avatar image CorruptedTNC Bunny83 · Feb 07, 2017 at 02:21 AM 0
Share

Thanks for the heads up, I started a lot of work before co$$anonymous$$g back to check this and have solved pretty much all of the problems as they've been popping up. It feels a bit hacky but it'll do.

2 Replies

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

Answer by dpoly · Feb 05, 2017 at 11:26 PM

I think you'll find that Unity always serialises the underlying field and never the property. For auto-implemented there isn't one, or at least not one that Unity can/will serialise.

Comment
Add comment · Show 1 · 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 Bunny83 · Feb 06, 2017 at 04:59 AM 0
Share

You're right. However there is a field for auto-properties though that field is private and can't be accessed in any way. Since Unity only serializes fields you can't tell it to serialize that field. Unity's serialization system doesn't care about properties. Properties are just methods and not data.

I know that other serialization system (such as the usual .NET serialization system) uses properties, but that has no relevance here.

avatar image
10

Answer by TimHW · Jan 24, 2019 at 11:14 PM

Added in C# 7.3, which Unity now supports:

You may attach attributes to the backing field of auto-implemented properties.


Example:

[System.Serializable]
public class Item
{
    [field: SerializeField] public int Id { get; private set; }
}


Note that if you are manually filling out variable names in something like JSON, the compiler-generated name is a bit gross for backing fields - <PropertyName>k__BackingField.


Example:
For an item with an Id of 16, it would be written in a .json file as follows:

{
    "<Id>k__BackingField": 16
}

Comment
Add comment · Show 3 · 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 TextusGames · Feb 15, 2019 at 07:15 PM 0
Share

Actually it works. Thanks.

avatar image mikawendt · May 02, 2021 at 12:19 PM 1
Share

This also applies to other Attributes like:

[field: Header("Some Title")] [field: Space]

avatar image dpoly · May 02, 2021 at 02:15 PM 0
Share

Bummer! I can serve Json very nicely using Asp.net Core, which serialises on property name to nice looking JSON. But I can't use the same C# to read that Json into Unity, because Unity wants fields not properties. Keeping the two in synch gets old quickly

I don't suppose there is any common ground?

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

288 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# how to setup a Binary Serialization 4 Answers

How do you use serialized values at creation? 1 Answer

Saving serialized data for iOS 0 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