Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# properties not getting serialized properly?

Discussion in 'Scripting' started by JonyderKiller, Sep 2, 2014.

  1. JonyderKiller

    JonyderKiller

    Joined:
    Sep 1, 2012
    Posts:
    10
    Hello everyone,

    I just started to get into interfaces and inheritance. I have a Interface called IItem with a property which looks like this:
    Code (CSharp):
    1. string name {get; set;}
    and a class called BaseItem. BaseItem extends MonoBehaviour and implements IItem, the implementation of the property looks like this:
    Code (CSharp):
    1. public virtual new string name {
    2.             get {
    3.                 return name.Equals("") ? gameObject.name : name;
    4.             }
    5.             set {
    6.                 name = value;
    7.             }
    8.         }
    and lastly i have a class called WeaponItem which extends BaseItem.
    Now when other parts of my code try to access WeaponItem.name Unity just crashes on me and a custom inspector which uses this code from the unity wiki. I can see the field just fine in the inspector but when I try to set it and play the scene the field gets set back to null and stays null when existing play mode, which I found out can happen if the field isn't serialized properly. So my question would be, if it is a serialization problem, how can i serialize a property? or if it isn't a serialization problem what could be the cause and what can I do to fix it?
    I know this is a rather complex question, but I hope you can answer it. Also if there are any design flaws with my code please let me know I am always willing to learn.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    For starters, Unity doesn't serialize properties. You can get around this by storing it as a private value elsewhere, using the property as an accessor for that value.

    Secondly, in your child class, won't "name = value;" just be recursively calling the name set function, causing a stack overflow?
     
  3. JonyderKiller

    JonyderKiller

    Joined:
    Sep 1, 2012
    Posts:
    10
    First thanks for your really quick answer and yes, you are right sorry about that one shouldn't stay up so late I guess. I modified the code:
    Code (CSharp):
    1. private string _name;
    2.  
    3. public virtual new string name {
    4.     get {
    5.         return _name;
    6.     }
    7.     set {
    8.         _name = value;
    9.     }
    10. }
    now i can read and write fine in my code and Unity doesn't crash on me anymore. But I am still having issues setting the property in the custom inspector. I also tried:
    Code (CSharp):
    1. weaponItem.name = EditorGUILayout.TextField("Name", weaponItem.name);
    but without success. Do you have any ideas on that?
     
  4. Conan_cs

    Conan_cs

    Joined:
    Apr 5, 2014
    Posts:
    16
    I don't understand the use of _name. Why you don't use name from MonoBehaviour instead?
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Ok... Rules of serialization in Unity;

    - Unity can only serialize fields, because properties are just methods.
    - Unity serialize all public fields that are in the types it can serialize. It ignores fields flagged with [NonSerialized].
    - Unity serialize private fields that are flagged with the [SerializeField] attribute, otherwise it ignores them.
     
    UnityCoach likes this.
  6. Conan_cs

    Conan_cs

    Joined:
    Apr 5, 2014
    Posts:
    16
    But you can modify the gameObject.name with the inspector!
    You don't need to customize your editor or to add any property.
    You just need to use the gameObject.name instead of your own _name attribute.
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Unless you want a name by MonoBehaviour, and a GameObject with multiple MonoBehaviour.
     
  8. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,924
    But, of course, the Unity API that serializes fields is named ... https://docs.unity3d.com/ScriptReference/SerializedProperty.html - and the docs there say:

    "for editing properties on objects"

    ...while the sample code clearly shows it only works on fields, not properties.

    (Is this mess is because whoever at Unity originally wrote this simply didn't know much C# code, and didn't know the difference between properties and fields?)

    (came here because: I wondered if Unity could serialize actual properties - as per the docs claim for the last 7 or so years! - and this was the top hit on Google. Testing suggests: no, Unity still can't handle properties)
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,964
    Last edited: Mar 19, 2020
  10. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,924
    Nice, but PropertyDrawers have never fully worked (Unity stopped updating them after they shipped the first version), so they come with so many problems I avoid them as much as possible.

    I know about Odin, but that also brings many issues of its own - the TL;DR is: "don't use properties, Unity doesn't support them - and don't be deceived by the docs that incorrectly say Unity does support them ... but if you wait for 2020.x, the new serializer might support them"
     
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,964
    Care to provide some examples?
     
  12. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,924
    You linked directly to an example that explicitly states that PropertyDrawers don't fully work, and is then followed by multiple posts from Unity's own staff confirming that serialized properties aren't fully supported yet, and 4 or 5 people bumping the post over time hoping to see it updated.

    At this point: I give up. I'm guessing you're using this stuff in a limited way and it works for you. That's great. But it *officially* doesn't work fully, and the rest of us have less narrow needs.