Search Unity

Backing field serialized despite using [NonSerialized]

Discussion in 'Scripting' started by Reahreic, Dec 9, 2020.

  1. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    Despite using [NonSerialized] on a member that deliberately hides another with the same name from the parent class, I get the occasional editor error message that unity is trying to serialize the same field twice. Specifically
    The same field name is serialized multiple times in the class or its parent class. This is not supported: Base(OnThingSelected) <assignedEffect >k__BackingField
    Shouldn't the field not be serialized, or even have a backing field to serialize, and why not throw the error for
    OnThingSelected
    as it's using the same behavior.

    It's likely I'm breaking a few polymorphism rules here, but without some major refactoring of the related systems I was hoping to just override the member.

    Simplified example for clarity:
    Code (CSharp):
    1. public class ThingLogics : MonoBehaviour {
    2.     public enum PhysicalEffect {
    3.         None,
    4.         Burn,
    5.         Freeze,
    6.         Acid
    7.     }
    8.     public enum VisualEffect {
    9.         None,
    10.         Sparkle,
    11.         Shine
    12.     }
    13. //More stuff
    14. }
    15.  
    16. public class ThingUI : MonoBehaviour {
    17.     [NonSerialized]//Assigned during generation by factory
    18.     public Action<ThingLogics.PhysicalEffect> OnThingSelected;
    19.     [NonSerialized]//Assigned during generation by factory
    20.     public ThingLogics.PhysicalEffect assignedEffect = ThingLogics.PhysicalEffect.None;
    21. }
    22. public class PrettyThingUI : ThingUI {
    23.     [NonSerialized]//Assigned during generation by factory
    24.     public new Action<ThingLogics.VisualEffect> OnThingSelected;
    25.     [NonSerialized]//Assigned during generation by factory
    26.     public new ThingLogics.VisualEffect assignedEffect = ThingLogics.VisualEffect.None;
    27. }
    28.  
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,065
    OnThingSelected
    is not affected because Unity doesn't support serializing delegates.

    Not sure what's happening with
    assignedEffect
    , maybe Unity checks and generates the error before the NonSerialized attribute is taken into consideration? I would try to create a simple repro project and then report it as a bug.

    But I suspect the error is caused by the shadowing (new). This effectively creates two
    assignedEffect
    fields, one in the base class and one in the derived class, which trips up Unity.

    Shadowing is pretty unusual, are you sure you want to shadow and not use a property with virtual/override (properties aren't serialized so that would remove the need for NonSerialized)?
    Or just let the factory assign the fields and not shadow/override them at all? From the code you've posted, it's not clear why that's necessary.
     
    bobisgod234 and Kurt-Dekker like this.