[SerializeField] vs public - Speed & Iphone

Hi,

As far as I can find there is no difference in using [SerializeField] or just putting your variable public when it comes to showing it in the inspector. Though I like to hide my fields by putting them private, and using properties where access is needed.

Though I was wondering if there are any differences? Especially, is one or the other faster for compiling or other usage?

Also, I read somewhere that [SerializeField] isn’t supported for iPhone, though couldn’t find any other evidence of this, so is this true?

Thanks !

P

Well public / protected / private is something totally different than SerializeField. public is a visibility keyword which doesn’t change anything on how a variable / field works in the first place.

However Unity automatically generates serialization-data for all public, serializable variables. This serialization-data adds extra overhead to the class when it’s stored in a scene or a prefab. SerializeField just allows you to have the same serialization feature for private fields. AFAIK you can even put the SerializeField attribute on public variables but it’s useless since Unity serializes them anyway.

The visibility keywords are only used to define who can see / access / change it and where.

In C++ there’s no difference how a member variable is stored in memory. “private” just tells the compiler to not allow you to access it but it doesn’t really prevent you from accessing it :wink:

In C# everything works different since you have RTTI(runtime type information) available but with reflection you’re also able to access private fields from outside.