Using C# attributes to Add Special Behavior to the Unity’s Inspector Window

Script with some variables
What the inspector shows

Adding C# attributes:

[Attribute]public int variable;
  • HideInIspector: allows us to hide a public variable from the inspector.
e.g.[HideInInspector]public int score = 0;
  • SerializeField: forces Unity to serialize a private field and show it in the inspector, very useful when you want to keep a variable private but allow the designer to modify the value.
e.g.[SerializeField]private int _shield = 3;
  • Min: set the min value for a float or int variable.
e.g.[Min(3)]private float _jumpForce = 8f;
  • Range: define a range of values for an int or float variable.
e.g.[Range(0,5)]private float _speed = 5f;
  • Header: allows to put a header above some fields in the inspector.
e.g.[Header(“Health Settings”)]
Variables with attributes



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store