Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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

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

  1. klarnorbert

    klarnorbert

    Joined:
    Jun 2, 2018
    Posts:
    3
    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:
    109
    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:
    321
    Completely agreed here.
     
  4. Gambit-MSplitz

    Gambit-MSplitz

    Joined:
    May 16, 2015
    Posts:
    989
    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

    Unity Technologies

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

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,412