#What's the purpose of [SerializeField] ?

1 messages · Page 1 of 1 (latest)

sand quarry
#

I looked at the Unity page that explains it but I still can't understand. Why need [SerializeField] if I can just set something to public instead of private ?

meager plover
sand quarry
#

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 ?

meager plover
#

that's not at all what encapsulation is about.

meager plover
#

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)

smoky bobcat
# sand quarry but if yes, what's the purpose of [SerializeField] if I can already be sure that...

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.

silk pewter
#

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"

brazen charm
wise stream
#

** 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

winged bison
#

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

dry notch
#

You can have both. [SerializeField, HideInInspector] will still save the values to the scene / prefab, but they’re not shown in the inspector.

wise stream