This page looks best with JavaScript enabled

Unity: Inspector Attributes

 ·   ·  ☕ 1 min read

Attributes are tags that can be added above a class or field. In Unity they change how your script appears in the Inspector without changing what the code does. Typically, I will use these as a
quality of life improvement that makes it easier to use the Unity Inspectors and setup Game Objects.

Here is a small MonoBehaviour that gives an example using four of them:

1
2
3
4
5
6
7
8
[RequireComponent(typeof(Rigidbody))]
public class Mover : MonoBehaviour
{
    [Header("Movement")]
    [Range(0, 10)]
    [Tooltip("Units per second")]
    public float speed = 5;
}

Sam Wronski
WRITTEN BY
Sam Wronski
Maker of things and professional software engineer. Lets make something awesome together!

Unity Inspector attributes

Attributes go in square brackets above a class or field and change how it shows up in the Inspector.

[RequireComponent(typeof(Rigidbody))]
public class Mover : MonoBehaviour
{
    [Header("Movement")]
    [Range(0, 10)]
    [Tooltip("Units per second")]
    public float speed = 5;
}
[RequireComponent(typeof(Rigidbody))]
on a class: Unity adds a Rigidbody when the script is added and stops you removing it while the script needs it
[Header("Movement")]
puts a bold label above the field in the Inspector, useful for grouping fields
[Range(0, 10)]
on a number field: shows a slider limited to this minimum and maximum
[Tooltip("Units per second")]
text shown when you hover over the field's name in the Inspector

Also useful

[SerializeField]
Shows a private field in the Inspector without making it public.
[Space]
Adds a gap between fields in the Inspector.
[ContextMenu("Reset Speed")]
On a method: adds it to the component's right-click menu so you can run it from the Inspector.

RangeAttribute in the Unity Scripting API