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

Do you use [SerializeField] a lot?

Discussion in 'Scripting' started by Deleted User, Mar 4, 2015.

  1. Deleted User

    Deleted User

    Guest

    One of the core principles of OOP is Encapsulation--a class's fields are only accessible through properties.
    But with the way Unity and its Inspector works, you have to use public fields to have them be serialized in the editor. This could lead to problem if from another class you try to access a public variable by accident.

    [SerializedField] can be used to show private fields in the Inspector.

    My question is: do you use [SerializeField] a lot or do you mostly make all your fields public?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Yes. All the time. In general you should expose everything through a property instead of just a straight public member.
     
  3. Deleted User

    Deleted User

    Guest

  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Yeah. They should change that :)
     
  5. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    ahahahahahaha...... that needs changing fast. They shouldn't be advocating a mess of public fields.

    I use SerializeField all the time. Most, if not all fields, are private - the ones I want in the inspector get SerializeField, and the ones I want exposed to other classes are done through public properties.
     
  6. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    242
    Honestly I find it all strange how they've set it up.

    I almost never want my public variables to be shown, as they're just raw data meant to be accessed by other classes, so I'm constantly having to write [hideInInspector]. Would love it if by default NOTHING showed in inspector, and we got to manually decide what's shown through [serializeField] :D
     
  7. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    I never use it.
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    Note, 'HideInInspector' doesn't stop it from getting serialized. It just doesn't display it in the inspector.

    Note:
    http://docs.unity3d.com/ScriptReference/HideInInspector.html

    If you're looking for it to not show up in the inspector and NOT be serialized. Use System.NonSerialized

    Code (csharp):
    1.  
    2. [System.NonSerialized()]
    3. public bool Blargh;
    4.  
     
    Juice-Tin likes this.
  9. Gallantl33

    Gallantl33

    Joined:
    Mar 2, 2015
    Posts:
    12
    Forgive my newbness, but why is this a thing? If you are coding in the fashion that unity recommends (using Components for specific functions), then you have to explicitly use GetComponent to touch any of the components in another script anyway.
    The only real reason I can see to use getters and setters is the functionality of "read only" or input checking, which, in my very very limited knowledge, is almost a failsafe designed so that if you mess up, it will throw an easy to find error.

    This is something I would really like to know about, not trying to be rude
     
  10. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Properties provide a level of abstraction that is very useful for encapsulation. Changing the inner mechanics of one class does not require you to change how other classes interact with that class. You may decide that you want events thrown when you change a variable, or change the underlying type you store the data in.

    Also, input checking is less of a 'failsafe for when you mess up' and more of a 'now I don't have to bother learning how your class works in order to use it'. It is very useful for teams. Or, if you later on decide to change your business logic and require different validation, you can easily update the setter.
     
  11. Saxi

    Saxi

    Joined:
    Jun 28, 2013
    Posts:
    381
    I use serialize field when I want to adjust something at run time. I avoid public like it is the plague, if something (in code) doesn't need access to it, it is private.
     
  12. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    I just want to add that The example project you can install with Unity 5 also uses [SerializeField] now instead of public fields, so it seems like the Unity devs are changing their styles to the more... common practices.
     
    Deleted User likes this.