Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development

Alan Mattanó
PLUS
Alan Mattanó
Courses Plus Student 12,188 Points

What is the difference between Public vs SerializeField and when to use it?

What are the advantages of using SerializeField? And When you are using one or the other.

Ps: Thanks Nick for your comment! as a beginner i do my best with my poor English.

3 Answers

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Alan Mattanó,

Great question! This can be confusing, because when you create a public variable, it will appear as a field in the Inspector. However, if you add the [SerializeField] attribute to a variable, it will also appear in the Inspector. So which should you use? The answer is a little bit nuanced, but allow me to explain.

Words like public, private, and protected are called access modifiers, and they change what's called the accessibility level of a class or a variable. You can read more about access modifiers in Microsoft's official C# language documentation here: https://msdn.microsoft.com/en-us/library/ms173121.aspx

Variables that are public can be accessed and modified by code outside the class. In general, you should always try to create variables with the lowest accessibility level necessary; i.e. private. There are many reasons for this, but one reason is that when you're working on a large team, or even by yourself, it can be easy to write code that accidentally modifies variables in other classes that shouldn't be modified. Most variables that you create really are only needed inside of the class where they're declared, and they should be private.

While it may be tempting to make variables public because Unity automatically makes them appear as a field in the Inspector, it's much better to make a variable private (for the reasons mentioned above) and then only use the [SerializeField] attribute if you need to access them through the Inspector. It might seem like that defeats the purpose of making it private, since values could be accidentally modified in the Inspector. However, this is much easier to find and debug than a rogue modification made somewhere in another script.

To read more about [SerializeField] you can read Unity's official documentation here: http://docs.unity3d.com/ScriptReference/SerializeField.html - However, I disagree with their statement that says "You will almost never need this." - They encourage the use of public variables for all serialization (which, at the risk of oversimplifying, you can think of as variables that appear as fields on components in the Inspector), but in my opinion and in the opinion of many other programmers, this is bad OOP practice. You should be using appropriate accessors, like private, and if you need to serialize those fields, then that's perfectly OK.

I realize that's pretty complex, so hopefully that all makes sense!

Alan Mattanó
Alan Mattanó
Courses Plus Student 12,188 Points

Thx Nick! I'm learning a lot here. Just to let you know , Looking Unity Unite 2015 Europe videos a (very low performance phone) game developer claim that Public field faster than the private [SerializeField] Link: https://youtu.be/4lAam8Marns?t=7m00s

jason chan
jason chan
31,009 Points

serialize field is the drag object onto GUI input field. It creates that field when you want to drag player or enemy. in html is input field. I hope that helps.

private - Only the current class will have access to the field or method.

protected - Only the current class and subclasses (and sometimes also same-package classes) of this class will have access to the field or method.

public - Any class can refer to the field or call the method.

https://en.wikipedia.org/wiki/Information_hiding

Emily Johnson
Emily Johnson
1,281 Points

Thank you for clarifying! I was super confused when reading the unity docs page.