7 Useful Unity Attributes That Make the Inspector Easier To Use

Elnur
Elnur
Dec 16, 2020 · 6 min read

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.

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, use SerializeField 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.

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!

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 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 used
  • maxLines define 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:

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:

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 only Parent 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:

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:

Star Gazers

“If you want to master something, teach it.”

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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