Stop making fields public just to see them.

There's a safer way to tune values in Unity.

Stop making fields public just to see them.

The problem

Public to the Inspector, and everyone else.

public class Player : MonoBehaviour{    public float speed = 5f;}

public shows it in the Inspector. It also lets every other script in the project change it.

Why it matters

Anyone can break it.

When something sets speed to zero, the bug shows up in Player, far from the code that caused it.

The Inspector
can edit it ✓
Every other script
can edit it too ✗

The fix

Private, but visible.

[SerializeField] tells Unity to save a private field and show it in the Inspector.

private+[SerializeField]

Visible to you, invisible to other scripts.

The fix in code

One attribute.

public class Player : MonoBehaviour{    [SerializeField] private float speed = 5f;}

You still tune it in the Inspector. Other scripts can't touch it.

Others need to read it?

Give them a read-only property.

[SerializeField] private float speed = 5f; public float Speed => speed;

Other scripts can read Speed, but only Player can change it.

The rule

Private by default. Serialize what you tune.

Tune it
[SerializeField]
Share it
public X => x;
public field
only on purpose