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

Question What-all does System.NonSerialized do?

Discussion in 'Editor & General Support' started by IIBearWithII, May 24, 2023.

  1. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    I know that it stops the editor from showing public variables in the inspector for scene objects. But it also seems to stop variables that have prefabs that were attached to them in the Project view from being used in the scene. All of a sudden the reference is null. Why is that?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    Serialization is the way Unity copies and saves pretty much everything. It is:
    • How data in your scripts in the scene are stored in scene and prefab files
    • How data is shown in the inspector
    • How data is copied when you copy an object using the inspector
    • How data is serialized into and out of objects when using JsonUtility.
    Basically, if something is serialized, the Unity engine can deal with the data in it. If it's not, it is invisible to the Unity engine and only exists in the managed memory of your C# scripts.

    If the reference is not serialized, it cannot be saved to scene or prefab files and Unity will not be able to save the reference anywhere.

    Variables that are
    public
    or marked with
    SerializeField
    will be serialized. Fields that are not public, or are marked with
    NonSerialized
    will not be serialized.
     
    IIBearWithII likes this.
  3. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Hmmmm thanks for the info. I wish the manual said as much. All it said was it stopped things from showing in the inspector.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    IIBearWithII likes this.
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    IIBearWithII likes this.