Decorator in Unity

Decorator in Unity

· 2 min read
Decorator in Unity

unity-attribute

[HideInInspector]

If you need a public variable in your script (so the other scripts can use this variable), but you don't want it exposed to inspector. See below example, the public hidenProperty it not visible in the inspector.

HideInInspector

[Header("string")]

Add a comment in the inspector for the variables used in the script. This is especially useful when you have tons of variables. See below example: two attributes, speed and myBag have their comments above in the inspector:

image--9-

[SerializeField]

On the opposite of the [HideInInspector], [SerializeField] makes a private variable of a script visible in the inspector.

SerializeField

[Space]

Make a space in the inspector, to organize the attributes and variables in the inspector:

Space

[Range]

Every time when we create int, float varibles, it would be better to put a range on it. As in some scenarios, it is not just us put the value here, there are might be some other people, such as testers (testers are friends 😎) want to play with different value and we don't want them to put some crazy value. i.e speed, weight, attack and etc. See following example of variable Speed:

image--6-


Reference