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 the
Header 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.
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
Note: Using public instead of
SerializeFieldattribute is not good practice. Whenever you want something to show up in Inspector, use
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
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.
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
Look how they are organized, you do not need to be confused between them!
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:
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:
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.
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
Firstly, we add this attribute before the string variable:
As you can see, there are 2 parameters:
minLinesdefine at least how many lines have to be used
maxLinesdefine how many lines have to appear at most. If there is more line than the
maxLines, 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:
minLines property is 2, so it has 2 lines by default.
maxLines property is 4. If we add 6 lines, for example, we will see a scroll button:
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
Now, let’s add
TestScript to the
Parent object and mark it as
Note: We added
TestScriptto the only
In the code, we mark the class as
Now, whenever we click to them, it will choose the
Parent object, even though it is an empty object:
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: