7 Useful Unity Attributes That Make the Inspector Easier To Use
When I am working with Unity, I have learnt some very useful attributes. For the first time, when I learnt them, I got surprised about how helpful they are. They help us in using the inspector easier, saving your time, and so on. For example, I have learnt theHeader
attribute that helps us to organize our variables in the inspector. Imagine, you have lots of variables and do not use Header
, how messy they are going to seem! Fortunately, we have these kinds of stuff in Unity. In this article, I am going to explain some of them to help you when you are working in Unity.
If you want to learn these great attributes and improve your working quality, let’s get started.
SerializeField Attribute
If you want to write better code, of course, one of the main points is Encapsulation. For example, if you have a variable, which you do not want to be accessed by another class, you mark it as private. In this case, it will not show up in the inspector. Because, Unity only serialize public variables, so that they are being visible in the inspector and their values can be changed. To display a private field in the inspector, we can use SerializeField
attribute.
Note: Using public instead of
SerializeField
attribute is not good practice. Whenever you want something to show up in Inspector, useSerializeField
attribute.
Let’s see a quick example here:
In the script, we have 3 different variables. Two of them are private, and the other one is public. We have marked the first private field with [SerializeField
, we have not marked the second private field with [SerializeField
, and the public field will automatically be displayed in the inspector. So, let’s look at how they are showing up in the inspector:
As you can see score
and myFloat
variables are displayed in the editor, and we can change their value, but speed
value didn’t show up there. That’s because we didn’t mark it with [SerializeField]
and it is private.
Header Attribute
Imagine you have lots of fields in the inspector, they all will be display mixed. You will end up with spending redundant time for searching some fields. In these situations, Header
attribute comes handy. Let’s have a look at to the code:
Firstly, I want to show you how fields are displayed before using Header
attribute:
After implementing Header
attribute:
Look how they are organized, you do not need to be confused between them!
Range Attribute
When we make a field to show up in the inspector, we can assign any value to them. If we want to restrict it, we can use Range
attribute. With this attribute is used, it will do both: restricting assignment between two number and making it to show up as a slider. Let’s have a look at the example code:
After that code, our variables will look like this:
Tooltip Attribute
Sometimes we want to have a description of the variables in the Inspector. In this case, Tooltip
attribute can be helpful. For example, let’s say we have a damage
value which should only be between 0 and 150. If we use this attribute when we hover over the damage variable we will see a description. Let’s see the example:
In the Inspector, it will look like this:
RequireComponent Attribute
Imagine we are working with the Rigidbody
component and we have to attach the script to the five different objects. Instead of adding Rigidbody
component one-by-one, we can use this handy feature. This attribute makes sure that whenever we want to attach this script to some object, it is added together with the component which we have declared. Let’s see the example:
In the script, we added RequireComponent
for the type Rigidbody.
When we added this script to the game object, Rigidbody
is also added.
Also, if we want to remove the Rigidbody
component, we will not be able to it. It will say:
It will prevent us from accidentally removing it and wasting time.
TextArea Attribute
Normally, when we declare a string variable which will be set in the Inspector, we get only 1 line. When we add long string, it continues to the only right which is not readable. It is displayed like this:
Now, let’s see it with the TextArea
Attribute:
Firstly, we add this attribute before the string variable:
As you can see, there are 2 parameters: minLines
and maxLines
.
minLines
define at least how many lines have to be usedmaxLines
define how many lines have to appear at most. If there is more line than themaxLines
, we can still add a new line. But instead of expanding, it will be controlled by scrolling (I will show it in a minute).
In the Inspector, it will be displayed like this without text:
Our minLines
property is 2, so it has 2 lines by default.
Our maxLines
property is 4. If we add 6 lines, for example, we will see a scroll button:
SelectionBase Attribute
Let’s say we have an empty parent object and a cube object which is a child of it. Like this:
In the scene view:
As a default, if we click to the object, it will choose Child
one:
Now, let’s add TestScript
to the Parent
object and mark it as SelectionBase.
Note: We added
TestScript
to the onlyParent
object.
In the code, we mark the class as SelectionBase:
Now, whenever we click to them, it will choose the Parent
object, even though it is an empty object:
Conclusion
To sum up, in this article, I explained 7 helpful Unity attribute which helps us when we are working. I hope you liked it.
If you want to read more get this link: