Who is allowed to change your health?

Right now, every script in your game.

Who is allowed to change your health?

The problem

A public field takes any number.

public class Player{    public int Health = 100;} // in any script, anywhereplayer.Health = 9999;

Any script can write any value, and the player never finds out.

The fix

Read in public. Write in private.

Make it a property with a private setter. Everyone can read it. Only its own class can change it.

get→everyone
private set→only Player

The fix in code

Changes go through one door.

public class Player{    public int Health { get; private set; } = 100;     public void TakeDamage( int amount )    {        Health -= amount;        if ( Health <= 0 ) Die();    }}

Damage has one way in, so the death check can't be skipped.

Watch out in Unity

Properties hide from the Inspector.

[field: SerializeField]public int Health { get; private set; } = 100;

The attribute lands on the hidden field behind the property, so Unity can show it.

The rule

Let everyone read it. Let one class write it.

Everyone reads
get
Only the class writes
private set
Changes go through
a method