Search Unity

[SerializeField] - "You will almost never need this."

Discussion in 'Documentation' started by klarnorbert, Jun 20, 2018.

  1. klarnorbert

    klarnorbert

    Joined:
    Jun 2, 2018
    Posts:
    22
    Hello guys!

    So I just browsing the Scripting API today, and found this questionable phase under the [SerializeField] documentation: "You will almost never need this.".

    Why would anyone write this? When learning OOP concepts and encapsulation, the first thing you learn is, don't use public variables, use instead setter and getter methods.

    Using [SerializeField], you can expose your private variables to the Inspector, without actually exposing it to other scripts.

    Imho, don't teach bad practices for beginners, especially at the offical API. Please change the documentation to this: "You will almost always need to use this, instead of using public variables (except some rare cases)."
     
    Last edited: Jun 20, 2018
    Sylmerria, joshcamas and rakkarage like this.
  2. Redux

    Redux

    Joined:
    Nov 10, 2011
    Posts:
    146
    Seconded, I use [SerializeField] extensively to manage the public interface of a class against it's serialization. Numerous state members which need serialization but should not be the concern of client code.

    Change this Unity, you're advocating for bad choices, and from a position of authority. This should not be in your documentation.
     
    Sylmerria and joshcamas like this.
  3. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    Completely agreed here.
     
  4. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I probably use SerializeField as much as I use any other keyword. Usage depends greatly on the type of project, but "you will almost never need this" has needlessly freaked out a few of my coworkers in the past, so I agree it should probably be removed.
     
  5. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334