Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Preventing something from being serialized

Discussion in 'Scripting' started by zedz, Aug 11, 2022.

  1. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    229
    I was under the impression that [System.NonSerialized] would do this be it doesn't, it still will serialize the data

    [System.NonSerialized] public List<GameQuest> gameQuests; <- Still gets serialized to disk !!!!

    I can stop it from being serialized by going [System.NonSerialized] private ... but I want to know, why on earth does C# ignore [System.NonSerialized] & serialize it in the above line?

    Cheers zed

    PS https://docs.unity3d.com/ScriptReference/NonSerialized.html
    Does not work, I noticed this years ago as well, juding by my comments in my code, WTF is this getting serialized when I'm telling it not to get serialized :)
    perhaps I need to do [System.NonSerializedPrettyPlease]
     
    Last edited: Aug 11, 2022
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,397
    [HideInEditor]
     
    TheDevloper and zedz like this.
  3. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    229
    OK I assume you mean [HideInInspector] as a google for HideInEditor returns nothing
    but it still serializes the field

    [HideInInspector] public List<GameQuest> gameQuests; <- serializes
    [HideInInspector, System.NonSerialized] public List<GameQuest> gameQuests; <- serializes
     
  4. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    507
    It works fine in my project (and I assume many others) so you should maybe open a bug report and send it to Unity so they can verify why it doesn't work in your specific case
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    FYI Unity doesn't need to see a bug report for this. HideInInspector does just that. As it states in the docs, it'll still serialize it but just doesn't show it in the inspector.
     
  6. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    507
    Yes, but my answer and the original question was about [System.NonSerialized]
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    Yes, if the OP is sure that is being ignored then for sure, report it as a bug.
     
    zedz likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    In the meantime I'd set it to private but if you want it public then make a public property to access the private field.

    It's also worth asking how you are verifying it's being serialized? Are you looking at the text serialization output? Are you sure it's resaving?
     
    zedz likes this.
  9. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    I can't replicate it. Can you share some some surrounding code?

    Code (csharp):
    1.  
    2. [System.Serializable]
    3. public class GameQuest
    4. {
    5.    public int id;
    6.  
    7.    public GameQuest(int id) { this.id = id; }
    8. }
    9.  
    10. public sealed class Help_NonSerialized : MonoBehaviour
    11. {
    12.    public List<GameQuest> SerializedList;
    13.  
    14.    [NonSerialized]
    15.    public List<GameQuest> NonSerializedList;
    16.  
    17.    [NonSerialized]
    18.    public GameQuest NonSerializedInstance;
    19.  
    20.    private void Awake()
    21.    {
    22.        Debug.LogFormat("SerializedList.Count: {0}", SerializedList.Count);
    23.        Debug.LogFormat("NonSerializedList: {0} Count {1}", NonSerializedList, NonSerializedList != null ? NonSerializedList.Count : -1);
    24.        Debug.LogFormat("Instance: {0}", NonSerializedInstance);
    25.    }
    26.  
    27.    private void Reset()
    28.    {
    29.        Debug.Log("Reset");
    30.  
    31.        SerializedList = new List<GameQuest>()
    32.        {
    33.            new GameQuest(0),
    34.            new GameQuest(1)
    35.        };
    36.  
    37.        NonSerializedList = new List<GameQuest>
    38.        {
    39.            new GameQuest(100),
    40.            new GameQuest(101)
    41.        };
    42.  
    43.        NonSerializedInstance = new GameQuest(200);
    44.    }
    45. }
    46.  
    debug.png

    And the prefab YAML saved in the project:
    Code (csharp):
    1.  
    2. MonoBehaviour:
    3.   m_ObjectHideFlags: 0
    4.   m_CorrespondingSourceObject: {fileID: 0}
    5.   m_PrefabInstance: {fileID: 0}
    6.   m_PrefabAsset: {fileID: 0}
    7.   m_GameObject: {fileID: 1765239488863784794}
    8.   m_Enabled: 1
    9.   m_EditorHideFlags: 0
    10.   m_Script: {fileID: 11500000, guid: 1e43c6c0e63ede840be50d19858c13a6, type: 3}
    11.   m_Name:
    12.   m_EditorClassIdentifier:
    13.   SerializedList:
    14.   - id: 0
    15.   - id: 1
    16.  
     
    Bunny83 and zedz like this.
  10. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    229
    I'm looking at the file on the disk, its updating with the right time stamp.and if I look at the file I can see the NonSerialized data there

    OK just tested
    [System.NonSerialized] public List<GameQuest> gameQuests; 4038656 bytes on disk
    [System.NonSerialized] private List<GameQuest> gameQuests; 3979240 bytes on disk

    saving with
    XmlSerializer serializer = new XmlSerializer(typeof(GameFileContainer));
    StreamWriter writer = new StreamWriter(Application.persistentDataPath + "/" + filename + ".xml" );

    IIRC XmlSerializer was not recommended for xml (there are better options?, though I've been using it for years so inertia on my part )

    OK tested if I save as binary then both files are the same length, hmmm so its when I save as xml
    that [System.NonSerialized] public is still being serialized, with a binary file it works as it should.

    PS: This ain't important as like I say I just change it to private and it works, I was just curious why it was not doing what the doc's said
     
    MelvMay likes this.
  11. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Potentially because the XmlSerialiser doesn't respect the NonSerialized attribute?

    I imagine it you used JSON this would also be a non-issue.
     
    MelvMay, zedz and oscarAbraham like this.
  12. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    229
    Thanks mate, OK searched a bit more yes it seems as if NonSerialized is ignored, other people were having the same issue.
    The solution is to if you want it to not be serialized with xml (at least this parser) is to use
    [XmlIgnore]
    and not [System.NonSerialized]

    And yes You're right I should be using json, I'm using code I wrote many years ago when xml was more widely used than json, I should update
     
  13. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    229
    Man my memory must be going, I spent 5 minutes trying to find out why it was still being saved even though I specify [System.NonSerialized] and then I remembered oh I think I've had this bug before :eek:

    and an update I did try json, but the resulting files were larger (for the same data) than xml so decided not to use it (file size/readbility was the biggest reason for me)