- Home /
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.
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
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.
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.
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.
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
}
This also applies to other Attributes like:
[field: Header("Some Title")] [field: Space]
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
![](https://koobas.hobune.stream/wayback/20220612110404im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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