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 Sorade · Sep 10, 2018 at 06:27 PM · serializationproperties

Properties not serializing properly

I am having the following problem.

For some reason Unity resets the values of properties in Play Mode or when the gameobject is made into a prefab.

In the example below t1 does not serialize properly whilst t2 does. Any idea what may cause the issue and how to fix it ?

 [SerializeField] // this is needed otherwise t1 doesn't show up at all in the inspector
 public TargetType t1{get; set; }
 public TargetType t2;
Comment
Add comment · Show 1
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 PizzaPie · Sep 11, 2018 at 01:37 PM 0
Share

As it didn't mentioned, Unity docs refer to fields as properties, which may cause the misconception that Unity can serialize c#'s properties.

2 Replies

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

Answer by taylank · Sep 10, 2018 at 07:17 PM

As far as I know auto-properties don't get serialized by Unity. You need a backing member for your get-set block. I'd go with:

 [SerializeField]
 private TargetType t1;
 public TargetType T1 {
      get {return t1;}
      set {t1 = value;}
 }
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 Sorade · Sep 11, 2018 at 11:03 AM 0
Share

Thanks for the suggestion. If I do that I can see but not edit the property in the inspector. I have to add T1 = t1 inside the OnValidate method for it to work. This seems to me that it would make it very inconvenient to code to interfaces rather than classes as I had planned to do. Is that indeed a no go when working with Unity ? Even though interfaces are a great alternative to multiple inheritance not being possible in C#.

avatar image taylank Sorade · Sep 11, 2018 at 03:23 PM 0
Share

You shouldn't need that extra code in OnValidate for the above code to work. t1 should be visible and editable in the inspector. Are you sure you are not somehow applying some kind of ReadOnly attribute on t1? Or do you have a custom editor for your class?

As for interfaces, yeah Unity does not serialize them. Polymorphic serialization in general is a non starter in Unity.

avatar image
2

Answer by Bunny83 · Sep 10, 2018 at 07:22 PM

Unity never supported serialization of properties. Only fields can be serialized. Attributes on auto properties belong to the property only. The implicit private backing field can't get any attributes. If you want to apply attributes to the backing field you can't use auto properties. So you have to implement the backing field yourself

 [SerializeField]
 private TargetType m_T1;
 public TargetType t1
 {
     get {return m_T1;}
     set {m_T1 = value;}
 }

Keep in mind that Unity's serialization system directly serializes fields. That means when changing a value in the inspector you actually change the field. The setter of the property will not be called. Again, Unity does not serialize properties at all.

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

96 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

Related Questions

(SOLUTION) How to serialize interfaces, generics, auto-properties and abstract System.Object classes? 3 Answers

How to get a List of all Variables in a Class 1 Answer

MonoBehaviour Properties not saving their values from Editor 2 Answers

Using DuplicateCommand and DeleteCommand with Properties 0 Answers

Property drawer stops variables from being read 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