Attributes
Tutorial
·
intermediate
·
+10XP
·
5 mins
·
(86)
Unity Technologies

Attributes are a way of tagging or labeling pieces of code. By the end to this tutorial, you’ll be able to mark your code with attributes using appropriate syntax
Languages available:
1. Overview
Attributes are a way of tagging or labeling pieces of code. You can then check your code for attributes using a technique called reflection and run code based on what you find.
By the end to this tutorial, you’ll be able to mark your code with attributes using appropriate syntax
Note: Reflection is beyond the scope of this introductory tutorial.
2. Before you begin
This tutorial is part of Beginner Scripting, a collection of basic introductions to a wide range of different programming concepts. If you want a more guided learning experience, try the Create with Code course or the full Junior Programmer pathway.
3. Attribute syntax
Attributes must be surrounded by square brackets. You can write them either ttributes before or above pieces of code. You can add attributes to many different pieces of code, including fields, classes and methods. You can also include parameters that affect how attributes behave.
Here are some examples of attributes added to code.
Field with an attribute
Class with an attribute
Method with an attribute
In the rest of this tutorial, you’ll explore the examples above in more detail.
4. The Range attribute
The Range attribute can be applied to integer and float fields which appear in the inspector. When number fields (either floats or integers) are given this attribute their entry in the inspector is replaced by a slider. The attribute takes two parameters: the minimum and maximum values the slider will go between.

Default variable field

How the use of the "Range" attribute changes the editor
5. The CustomEditor attribute
You can use the CustomEditor attribute to match a Unity Object to an editor script that you have created. When you give a class definition (one that inherits from the Editor class) this attribute, its code will be used to display its target in the Inspector.
The CustomEditor attribute takes one parameter: the type of the target for which the attributed class is the editor. Normally this takes the form of a typeof expression.
In the example below, the PlayerScriptEditor will target instances of PlayerScript:
6. The InitializeOnLoadMethod attribute
You can use the InitializeOnLoadMethod attribute to mark a static method so that it is called when the Unity Editor is first opened. Creators typically use this attribute to set up editor tools.
In the example below, the Initialization method will be called when the Unity Editor is first opened:
7. Summary
In this tutorial, you learned the basics of attributes in C# scripting, including the appropriate syntax for marking pieces of code with an attribute. You also reviewed three common attributes: Range, CustomEditor and InitializeOnLoadMethod.
As you continue your journey as a creator, attributes will become more relevant and will help you to work as efficiently as possible.