Inspector Attributes
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 10 of 67.
Attributes on a field change how the Inspector shows it, without changing what the code does. Four come up in almost every Unity project:
[Header("Movement")]puts a bold title above the fields that follow, to group them.[Tooltip("Units per second")]shows that text when the mouse rests on the field's name.[Range(0f, 10f)]draws a slider, and the Inspector keeps the value between 0 and 10.[HideInInspector]hides a public field from the Inspector, while other scripts can still use it.
[Header("Movement")]
[Tooltip("Units per second")]
[Range(0f, 10f)]
[SerializeField] private float speed = 4f;
[HideInInspector] public int score;[Range] only limits what can be typed or dragged in the Inspector. Code can still set speed = 25f, and nothing stops it, so a script that needs a limit enforces it in code too.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unity Scripting Basics
1Your First Script
Hello, UnityPlace the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube4Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot7Project: Catch the Star
The CatcherA Falling Star5Transforms and Hierarchy
Parents and ChildrenScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon