#What's the purpose of [SerializeField] ?
1 messages · Page 1 of 1 (latest)
Because it's bad form to use public variables for the most part due to the principle of encapsulation.
I'm gonna check it out
So encapsulation is just a class to avoid having two variables with the same name making bugs ?
but if yes, what's the purpose of [SerializeField] if I can already be sure that I won't get any bugs in my code? What does this do clearly ?
no. Did you read the article?
that's not at all what encapsulation is about.
The Benefits of Encapsulation
Data Protection: By making data private, you prevent accidental or intentional modification from outside the class, ensuring data integrity. Code Reusability: Encapsulated classes often boast greater reusability because they function as self-contained units with clearly defined interfaces. Improved Maintainability: Changes to the internal implementation of a class are less likely to affect other parts of the program due to encapsulation.
(I edited out the "security" point because it's really not true or relevant)
You will never be sure you don't get any bugs in your code. The purpose of private is to specify a variable that is used only by the script it is declared in, pure and simple. SerializeField is there just to extend this approach to non-code elements in Unity like prefabs, gameobjects etc.
You can just slap a public on everything, but it's like saying you give keycards to the whole building to all of your employees because you're sure they won't go into restricted areas. They're not meant to, but they still can. As a programmer it's a good practice to just make things have access only to things you're sure they need.
To follow up on this, it's infinitely easier to find out why something's not working, and even to make sure things work first try, if you can look at your code and say
"This bit cannot access this variable"
As opposed to
"This bit shouldn't access this variable"
[SerializeField] is a thing unity created, it's not a replacement/alternative for public.
Just because public float Foo and [SerializeField] float Bar both show up in the inspector, doesn't mean that these both behave in the same way when it comes to writing the code.
Nothing outside of this class will have any way of getting the value of Bar, it isn't public
** Basic Knowledge **
[SerializeField] private float something // Only allowed to be accessed in this script can be seen in editor to help visualize things
[SerializeField] float something // same as before ^
public float something // can be seen in editor and can be accessed via other code and modified from other code easily.|
[SerializeField] Help see private code in editor
[Header("Name")] Help label things in editor
** Advanced knowledge**
@winged bison mentioned
SerializeField serializes the values so they are stored in the file itself, Unity shows serialized values in the inspector
Kind of not right. SerializeField serializes the values so they are stored in the file itself, Unity shows serialized values in the inspector
It does what you say but serializefield isn’t for viewing things, thats a byproduct
You can have both. [SerializeField, HideInInspector] will still save the values to the scene / prefab, but they’re not shown in the inspector.
I was mainly giving a jist, you are correct