As most people probably know, public fields in Unity scripts (MonoBehaviour) are automatically displayed in the editor and serialized/saved to the scene file. Protected and private fields are not displayed or serialized by default.

Interestingly, there are some attributes that can influence the default behaviour:

public class EditorVisibility : MonoBehaviour
{
	// Fields decorated with the HideInInspector-attribute do
	// not show up in the editor but will be serialized
	[HideInInspector] public int invisible = 5;

	// Fields decorated with the SerializeField-attribute are
	// visible in the editor and will be serialized
	[SerializeField] private int visible = 5;

	// Properties can't be serialized
	// (You can serialize the backing field of a property, however)
	[SerializeField] public int MyProperty { get; set; } // does not work

	// Fields decorated with the NonSerialized-attribute do
	// not show up in the editor and won't be serialized
	[System.NonSerialized] public int invisibleNotSerialized = 5;
}

But which attribute ist useful in which case?

HideInInspector is especially useful if there's an editor script that processes the field from the outside. This way, the field can be hidden from the user while the editor script has access to it. It is still being serialized to the scene.

SerializeField should be used quite often as a replacement for public fields, because most of the time it does not make much sense that other scripts can access your fields directly. By using SerializeField the user is able to edit private and protected fields in the editor while other scripts are unable to manipulate them at runtime (, which could lead to unexpected behaviour). Fields with the SerializeField-attribute are serialized to the scene file, obviously.

System.NonSerialized is similar to HideInInspector, but values are not being serialized to the scene. This can be useful for fields that should be publicly accessible from other scripts, but are irrelevant to the user. Public properties are a much better choice for this scenario, because they are not being serialized by default.