Modifying Variables in Unity with Attributes

Unity Attributes are additional modifications we can make to variables, allowing us to work better within Unity. They provide additional control over variables, specifically in the Unity Editor.

Some of the most useful attributes are:

[SerializeField]

Forces Unity to serialize a private field, making it visible/editable in the Inspector, and saved with the scene or prefab.

[SerializeField] 
private int myPrivateField;

[Header("Text")]

Adds a header above fields in the Inspector.

[Header("Player Settings")]
public float speed;
public int health;

[Tooltip("Description")]

Displays a tooltip when hovering over the field in the Inspector.

[Tooltip("The player's movement speed in units per second")]
public float speed;

[Space(pixels)]

Adds vertical space between fields in the Inspector.

public float speed;
[Space(10)]
public int health;

[Range(min, max)]

Creates a slider in the Inspector for numeric values.

[Range(0, 100)]
public int health;

[TextArea]

Creates a multi-line text field in the Inspector.

[TextArea]
public string description;

Last updated