What is the difference between [SerializeField] and public?

What is the difference between [SerializeField] and public?

For example;

[SerializeField] int num1 = 0;

and

public int num1 = 0;

They both give the same output, controls in the Unity inspector tab, so what is the difference between them?

1 Like

Great question!
You’re right that they both make the variable visible in the inspector. The big difference is that, over on the coding side, the serialized field you’ve set up is still a private variable - so you won’t be able to directly access it from other scripts.

When setting the access levels for your variables your really asking two questions, do I want to:

  1. access this variable from another script - e.g. private or public
  2. edit the value in the inspector - e.g. [SerializeField] or [HideInInspector]

I hope that helps.

1 Like

Yes, thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms