- Home /
Is there any difference between [SerializeField] and a public variable?
Hello,
I was wondering if there is any real difference between [SerializeField] and a public variable.
I use a lot [SerializeField] to expose private variables and [HideInInspector] to force serialization but no to show it in the inspector, mainly because I prefer to have private variables and public properties for them as a general rule to keep things better organized in my code (at least for myself)
But I was wondering if there is any big performance hit, or any other thing hidden inside by using this style of exposing variables.
Thanks a lot.
Italo F. Capasso AKA "Edwod Grant"
Answer by Tetrad · Jul 14, 2010 at 06:16 AM
As far as I can tell with our usage of it, there is no difference for what you want to use it for. Personally, I'm with you where I prefer to keep my variables private, or at least hidden behind properties, and using [SerializeField]
explicitly for things that show up in the inspector.
It does make porting code to the iPhone (at least currently) a pain, though, since that property isn't in Unity iPhone.
Thanks a lot, the tidbit about iphone is very useful too :) ill keep note on that wherever I program on the iphone.
Hi Tetrad, I couldn't find any info regarding SerializeField not working on the iOS. Would you please let us know where that information is from? Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613064656im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
When to use private var. and public var.? 4 Answers
Making a variable work in two separate objects? 4 Answers
[C#] Assets/BreakObject.cs(20,62): error CS0165: Use of unassigned local variable `broken' 1 Answer
Avoid public variable to get overwritten by the Inspector 1 Answer
Variable Not Changing In Method 1 Answer