Search Unity

Question Private field serialization.

Discussion in 'Scripting Dev Blitz Day 2023 - Q&A' started by DiacPaulAlexandru, Feb 23, 2023.

  1. DiacPaulAlexandru

    DiacPaulAlexandru

    Joined:
    Oct 20, 2020
    Posts:
    14
    Why does unity serialize private fields in editor and is there any way to disable this for getting better domain reload perf? (a bit unrelated question)Are there any plans for TypeCache at runtime, maybe a package?
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    I'm not sure im following. Unity has a [SerializeField] attribute... which is optional and not just auto implmented... Perhaps im missing some information.
     
  3. DiacPaulAlexandru

    DiacPaulAlexandru

    Joined:
    Oct 20, 2020
    Posts:
    14
    While running editor unity serializes private (not marked wit SerializeField) fields, if you have odin inspector this is very easy to observe by marking a private non serialized field with [ShowInInspector] then go inside the editor set a value to the field then trigger a script recompilation, what you will notice is that unity preserved the value you set before domain reload.
     
    Homicide likes this.
  4. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    Ahh, i see, good info (i dont own odin). Cheers.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    This is, afaik, to support the hot reload feature of Unity that nobody uses because it's so brittle and undocumented.
     
    CodeRonnie likes this.
  6. DiacPaulAlexandru

    DiacPaulAlexandru

    Joined:
    Oct 20, 2020
    Posts:
    14
    Yes that's why i was asking for a way to disable it since it never helped me or the team i am in, it just adds to domain reload times.
     
    TeodorVecerdi likes this.
  7. TeodorVecerdi

    TeodorVecerdi

    Joined:
    Jul 31, 2017
    Posts:
    1
    I second the idea of having a way of disabling this functionality.

    In my case this behavior causes warnings to be logged to the console on every recompilation because of private non-serialized fields with the same name in a base class and derived class.
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    You can decorate these fields with
    [NonSerialized]
    and Unity will ignore them during reloads.
     
  9. ProgrammerJens

    ProgrammerJens

    Joined:
    Jun 3, 2013
    Posts:
    12
    Sorry but serializing private fields by default is downright broken behavior by Unity and it did not use to be like this.

    How can we ever start from scratch when private variables are remembered between play sessions? Spamming my code with [NonSerialized] on every single variable I have?
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    They're not. At least not for monobehaviours and other scene objects.

    For scriptable objects they will likely persist. But this has always been the behaviour of assets.
     
    Last edited: Oct 5, 2023