Inspector Attributes
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 17 of 85.
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.
All lessons in Unity Scripting Basics
1C# Basics in Unity
Hello, UnityVariablesDecimals Need an fText in the ConsoleVariables That RememberIf and ElseCompare NumbersRecap: Coin Collector2Make It Move
Place the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube5Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot8GameObjects and Components
References in the InspectorGet a ComponentMissing ComponentsAdd a ComponentRequireComponentShow and HideRecap: Light Switch6Transforms and Hierarchy
Parents and ChildrenHoist the FlagScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon9Project: Catch the Star
The CatcherA Falling StarPractice on your own: Unity playground