• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Overcast · Oct 06, 2013 at 10:58 AM · inspectorpublicprivateserializefield

Using [SerializeField] vs public

Hi Unity community.

I just have a quick question regarding the use of [SerializeField]. I have been using this a lot on private variables (c#), but have noticed a lot of other people seem to just make all their fields public when they want the inspector to display them.

I would have thought this was less than ideal practice, since often you're exposing something only so you can edit it in the editor, not so that any other object could technically alter its state.

Even the Unity manual suggests "you will almost never need this".

So am I missing something here - is there some other reason to make all your properties public when they ordinarily wouldn't be? Is there a reason that [SerlializeField] isn't considered the norm for exposing fields in the editor?

Thanks for your time.

Comment
juliuslaak
Liinnkk
punzer_ta
unity-101
JoRouss
xBlueBear
DougRichardson
chapel701
bguild
bunnynsnake
mani3307
guitarjorge24
lleon79
tylerwongj
cgkaransahu
And 4 more...

People who like this

19 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Jamora · Oct 06, 2013 at 12:56 PM

There is no other reason to make your variables public than to make them visible in the inspector (aside from making them serializable... but SerializeField does that).

I suppose the reason [SerializeField] isn't the norm in exposing variables is because Unity doesn't want to be only a tool for programmers so people who don't know programming (artists or modellers etc.) could make their own games.

I personally think it's lazy of Unity to not have made a property drawer for public properties. (Ironic, isn't it?)

Comment
Overcast
TrickyHandz
Thorny2000
ilya_ca
R4mbi
Harinezumi
xBlueBear
chapel701
bunnynsnake
rafaelurben
asafsitner

People who like this

11 Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Overcast · Oct 08, 2013 at 03:39 AM 2
Share

Thank Jamora, I think this mostly answers my question, I just don't personally like using public fields out of convenience in the inspector, as it seems very non OOP regarding encapsulation.

Also, cheers re the link to drawers, never had stumbled across these - seems like a great middle ground between custom editor classes for small teams which don't need super pretty front end inspector panels

avatar image Doeko · Oct 08, 2013 at 08:45 AM 1
Share

Note that if you just want to "see" a field (not edit it in inspector) it's much more practical to use the debug inspector than to make them public.

avatar image Nullmind · Nov 27, 2016 at 08:31 AM 0
Share

@Jamora So how do you reference an object if the field is just private?

Should I use the GameObject.Find or varName = GetComponent()?

With the GetComponent methods I'm ok since you can access to the other components in the same GameObject, but what about GameObject.Find?

As far as I know these are expensive.

avatar image Bunny83 Nullmind · Nov 27, 2016 at 03:56 PM 21
Share

Had to split the post ^^.

There are several "things" you can do to a variable declaration that changes one or multiple of the three mentioned things:

 /*
                   | available from other classes  |  serialized  |  visible  |
 ------------------------------------------------------------------------------
 [HideInInspector] |               -               |      -       |    No     |
 ------------------------------------------------------------------------------
 [NonSerialized]   |               -               |     No       |    No     |
 ------------------------------------------------------------------------------
 [SerializeField]  |               -               |     Yes      |    Yes    |
 ------------------------------------------------------------------------------
 public modifier   |              Yes              |     Yes      |    Yes    |
 ------------------------------------------------------------------------------
 private,protected |              No               |     No       |    No     |
 internal modifier |                               |              |           |
 ------------------------------------------------------------------------------
 */

I ordered the first column by precedence. So if multiple apply to the same variable, the top most overwrites lower things.

Of course there are a few combinations that doesn't make any sense or are mutual exclusive. Like for example:

  • public / private / protected / internal are mutual exclusive

  • private + HideInInspector --> does not change anything

  • public + SerializeField --> does not change anything

  • NonSerialized + SerializeField --> NonSerialized dominates, SerializeField has no effect

Note: Things can only be visible in the inspector if they are serialized. If something is not serialized it will never show up in the inspector.

Though there is one special combination that involves 3 of the above that does actually make sense:

  • private + SerializeField + HideInInspector

This will make the variable encapsulated in the OOP sense. It also prevents the user from seeing / modifying the value in the inspector but the value is still serialized in the editor. However it can only be modified through methods / properties of that class.

avatar image DougRichardson Bunny83 · Nov 15, 2017 at 09:26 PM 2
Share

You should break this comment into it's own answer to this question. It's more accurate and informative than the accepted answer.

Show more comments
Show more comments
avatar image tarahugger · Jul 03, 2017 at 11:01 AM 0
Share

I truly do not understand why they would when basing this in c# not adhere to well established c# coding conversions. People programming outside of games will no doubt be annoyed and frustrated but those trying to take their skills elsewhere will likewise find it difficult. Why not just teach things properly from the get-go?

avatar image Bunny83 tarahugger · Jul 03, 2017 at 01:00 PM 1
Share

I'm not sure what you mean. First of all Unity is not "based" on C# or .NET / mono but is written in C++. Mono and C# are just used as managed scripting environment. So certain engine mechanics are based on C++ and have to adapted in order to be used from the scripting environment. That includes the ability to "destroy" objects which doesn't exist in the managed world.

Most other rather unusual practises are mainly used for performance. Unity's object system is component based. That means it encourages flat class hierarchies. Unity also doesn't use the normal virtual / override mechanics mainly for performance benefits. The kind of C# that is used is just plain C#. It can even be compiled by any other C# compiler (as long as the C# version isn't too high).

What exactly do you see in Unity that does not "adhere to well established c# coding conversions"? Again, the serialization system in Unity is C++ based, not .NET / mono. The system need to serialize things which are not based in the .NET / mono world.

avatar image

Answer by Bunny83 · Nov 27, 2016 at 03:55 PM

Jamora's answer might be a bit misleading. What he wanted to say is that the only thing that the SerializeField attribute and the "public" access modifier have in common is that they will make a variable "visible in the inspector" and serialized.


There are different things you have to decide which have almost nothing to do with each other in the first place:


  • Should this variable be serialized?

  • Should a serialized variable be editable in the inspector?

  • Should other classes be able to access this variable from outside


The first question is only about if the value of the variable should be serialized or not. The second question belongs to the last last one. A value should only be visible in the inspector when the user should be able to set a value in the inspector. The last question is a pure OOP question. You might want to read up on Encapsulation.


There are several "things" you can do to a variable declaration that changes one or multiple of the three mentioned things:

 /*
                   | available from other classes  |  serialized  |  visible  |
 ------------------------------------------------------------------------------
 [HideInInspector] |               -               |      -       |    No     |
 ------------------------------------------------------------------------------
 [NonSerialized]   |               -               |     No       |    No     |
 ------------------------------------------------------------------------------
 [SerializeField]  |               -               |     Yes      |    Yes    |
 ------------------------------------------------------------------------------
 public modifier   |              Yes              |     Yes      |    Yes    |
 ------------------------------------------------------------------------------
 private,protected |              No               |     No       |    No     |
 internal modifier |                               |              |           |
 ------------------------------------------------------------------------------
 */

I ordered the first column by precedence. So if multiple apply to the same variable, the top most overwrites lower things.


Of course there are a few combinations that doesn't make any sense or are mutual exclusive. Like for example:


  • public / private / protected / internal are mutual exclusive

  • private + HideInInspector --> does not change anything

  • public + SerializeField --> does not change anything

  • NonSerialized + SerializeField --> NonSerialized dominates, SerializeField has no effect


Note: Things can only be visible in the inspector if they are serialized. If something is not serialized it will never show up in the inspector.


Though there is one special combination that involves 3 of the above that does actually make sense:


  • private + SerializeField + HideInInspector


This will make the variable encapsulated in the OOP sense. It also prevents the user from seeing / modifying the value in the inspector but the value is still serialized in the editor. However it can only be modified through methods / properties of that class.

Comment
DougRichardson
macarrasco
ghellee
Michael-Ryan
bguild
bunnynsnake
schinois
blackpawninja
guneyozsan
mani3307
GetBrinxed
Malapropos
Trey_Yi
RemiSoleil
correia55
And 21 more...

People who like this

36 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image guneyozsan · Jun 11, 2018 at 07:04 PM 0
Share

Thanks for mentioning this combo: "private + SerializeField + HideInInspector". I never thought about it.

avatar image Tortuap · Apr 18, 2022 at 12:05 PM 0
Share

A minor mistake in your table : internal allows access from other classes (of same assembly).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Avoid public variable to get overwritten by the Inspector 1 Answer

When to use public or private variables for the inspector? 3 Answers

Custom Inspector for an array of a serialized class 0 Answers

Starting Play mode resets values for serialized class in the Inspector 0 Answers

Public and SerializedField's not showing in inspector 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges