Search Unity

When to use [SerializeField] and why?

Discussion in 'Scripting' started by vexe, Jun 3, 2013.

Thread Status:
Not open for further replies.
  1. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    I'm kinda new to the concept of serialization - I read about it in the wiki, in stack overflow and in the Unity scripting reference - kinda gave me an idea - but I'm still not sure why and where would I use it in my game.

    An example with some explanation would be appreciated - thanks in advance.
     
    aymusbond, frost4mage and agconti like this.
  2. tylernocks

    tylernocks

    Joined:
    Sep 9, 2012
    Posts:
    256
    I am not completely sure but when I use it, it's to make private variables visible in the inspector.

    For Example:

    Code (csharp):
    1. public int Speed = 10 //This Wil Show In The Inspector
    Code (csharp):
    1. private int Speed = 10 //This Will Not Show In The Inspector
    Code (csharp):
    1. [SerializeField]
    2. private int Speed = 10 //This Will Show In The Inspector
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Serialization is the process of taking an object in ram (classes, fields, etc...) and making a disk representation of it which can be recreated at any point in the future. When you apply the SerializeField attribute to a field, it tells the unity engine to save/restore it's state to/from disk. You mostly use serialization for the editor, and especially when building your own editor windows and inspectors.

    For most games (especially simple ones) PlayerPrefs is sufficient for saving state to disk.
     
    asig396, sawnch, AB498 and 35 others like this.
  4. jewelRana

    jewelRana

    Joined:
    Apr 19, 2017
    Posts:
    1
    [Serializefield] is just use for showing a private variable's value on Inspector. You can easily change the value like public variable. But None can access this value from another script or places.
     
    honor0102, Jason210, GloCZ98 and 15 others like this.
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    No, you can with the Reflection API.
     
    harlandpj and BanzBowinkel like this.
  6. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    what is the point of even saying this, for 99% of uses cases if you are using reflection to get private members your approach is flawed.
     
  7. GoodGuyPascal

    GoodGuyPascal

    Joined:
    Jun 19, 2018
    Posts:
    1
    Hey y'all, sorry for continuing the discussion :p but I just have to ask, what's the point of using [SerializeField] on a private variable to make it appear on the inspector when you can just make that variable public?

    Thanks!
     
  8. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    If it's public any code can change its value, if it's private only special code like unity deserilizers or other code utilizing reflection.

    It's more robust if external code cant mutate the object
     
  9. baralianarmy

    baralianarmy

    Joined:
    Mar 3, 2019
    Posts:
    1
    Hold up. If SerializeField is used to make a private field appear on the inspector, why not just change the field to public instead? I am super confused.
     
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    Just in case anyone else casts Raise Dead on this thread, restricting access to a variable or method by setting it to private instead of public is primarily done to minimize debugging effort as the fewer things there are accessing a variable or a method the less chances you can introduce a bug.
     
  11. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    It's mainly for teams working on the same stuff, having something private means "don't modify this value from outside the class" or something alike, I work alone so there's less chance of that happening, depends on your work environment.
     
  12. HermannOberth

    HermannOberth

    Joined:
    Mar 7, 2016
    Posts:
    2
    Also, even if you're not working on a team: you right now vs you in 2 years vs you in 4 years are like completely different people.

    If you write some kind of app or game as a solo programmer and it isn't a throwaway project that you just write once and then forget about, then you will be presumably debugging and refactoring and extending it for the next 2-4 years. Future versions of you will appreciate proper API design and encapsulation done right the first time.
     
  13. NathoSteveo

    NathoSteveo

    Joined:
    Dec 7, 2019
    Posts:
    7
    Entertaining thread : )
     
  14. UnityMaru

    UnityMaru

    Community Engagement Manager PSM

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    Please don't. Necroposting is incredibly frustrating.
     
    efge, OghamTheBold, Fenikkel and 4 others like this.
Thread Status:
Not open for further replies.