You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Attributes in Unity


There are some quite frequently used attributes in the Unity while writing games.

#1 [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.

#2 [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:

#3 [SerializeField]

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

#4 [Space]

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

#5 [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: