Why would you need Attributes in unity?
Is there a list of all the attributes in Unity?
What are the uses of those Attributes in Unity??
Are attributes necessary in Unity?
What are Attributes??
Attributes are markers that can be placed above a class, property, or function in a script to indicate a special behavior. Attributes can apply to variables, functions, or even an entire class. It helps you create write more efficient and flexible code.
For example, you can assign a float value using a slider and show private variables in the Inspector.
List of Attributes:
- Header
- Space
- Tooltip
- Range
- TextArea
- Multiline
- SerializeField
- HideInInspector
- ContextMenuItem
- ContextMenu
- HelpURL
- RequireComponent
- SelectionBase
- AddComponentMenu
- ExecuteInEditMode
Let’s understand all attributes in depth with a practical demo.
Setup 3D Unity Project
- Open a new unity project with a 3D template.
- Assign a project name and location.

- This is our Unity IDE.

Create a Scene with 3D Object
- Add a Sphere – 3D Object in the scene.

- Add 1 capsule as child of the sphere object as shown in the image below.

Create a new C# Script
Now it’s time to create the script for better understanding of attributes.
- Go to the Assets window and right-click.
- Create->C# Script to create a new script with the name “AttributesDemoScript”.

- Assign this script to the Sphere GameObject.
- Double click on Script to open the script in the editor.
Open Visual Studio Code or Visual Studio Community 2019
If you don’t have it installed, then you can download from the official website.
https://code.visualstudio.com/
Header Attribute in Unity3D
- This attribute is useful by adding a header above fields in the Inspector.
- Syntax : [Header (“String”)]
- Example:
[Header ("Sphere variable")]
public GameObject ball;
- Output:

[Header ("Sphere variable")]
public GameObject ball;
[Header ("======Capsule variable======")]
public GameObject capsule;
public Vector3 capsulePosition;

Space Attribute
- This attribute is creating a space between fields.
- Syntax : [Space (“int”)]
- Whereby the value you pass is the padding in pixels
- Example:
[Header ("Sphere variable")]
public GameObject ball;
[Space (50)]
[Header ("======Capsule variable======")]
[Space (50)]
public GameObject capsule;
public Vector3 capsulePosition;
- Output:

Tooltip Attribute
- This attribute creates a tooltip, that appears once you hover over the variable with the attribute
- Syntax : [Tooltip (“text”)]
- Example:
[Tooltip ("This variable represent 3 dimension position values of Capsule ")]
public Vector3 capsulePosition;
- Output:

Range Attribute
- This attribute is used to give you a slider, to select values within a range, directly from the Unity Editor
- Syntax : [Range (float value ,float value)]
- Example:
[Range (0.0f, 25.0f)]
public float capsuleScale;
- Output:

TextArea Attribute
- The TextArea attribute is useful when you have to add a long string (long text, message) in a string variable via the editor.
- **Syntax : [**TextArea]
- Example:
public string aboutSphere;
[TextArea]
public string aboutCapsule;
- Output:

- Now you see the difference between two text objects.
- TextArea provides the box that we use to type string values directly in the Inspector.
Multiline Attribute
- The multiline attribute works the same way, as the TextArea to store long text values.
- The only difference is the horizontal and vertical wrapping of text. TextArea types text like a paragraph so when the line ends it will automatically move to the next line. But Multiline continuously displays data in the same line.
- Syntax : [Multiline]
- Example:
[Multiline]
public string location;
- Output:

SerializeField Attribute
- The SerializeField attribute is used to show private variables in the inspector.
- **Syntax : [**SerializeField ]
- Example:
[Header("This is a private variable that will be made available in the editor")]
[SerializeField]
private int objectNumber;
- Output:

HideInInspector Attribute
- HideInInspector attribute is used to hide public variables in the inspector.
- **Syntax : [**TextArea]
- Example:
[HideInInspector]
public bool isEnd;
- Output:

ContextMenuItem Attribute
- When you assign the ContextMenuItem attribute to any variable. It will display a menu option when right-clicking the variable in the Inspector.
- **Syntax : [**ContextMenuItem (“string text”,”Method name”)]
- Example:
[Header("Right click this variable name!")]
[SerializeField]
[ContextMenuItem("Get a random scale", "RandomScale")]
private float randomScale;
void RandomScale()
{
randomScale = Random.Range(0.0f, 5.0f);
}
- Output:

- As you can see, this attribute passes 2 parameters -one for text message and another for the method name.
- When you click on the variable it will show button on the menu. If you click on this button it will call the method which is passed in the parameter.
ContextMenu Attribute
- The ContextMenu attribute will create a menu item when right-clicking the component instead of a specific variable. After selecting the menu option, the corresponding function will perform its task.
- Syntax : [ContextMenu (“string text”)]
- Example:
[ContextMenu("isEnd is true")]
void ChangeBool()
{
isEnd=true;
}
- Output:

HelpURL Attribute
- The HelpURL attribute will use to links, that send the user to e.g. the documentation. This attribute must be added above the class definition.
- **Syntax : [**HelpURL (“URL”)]
- Example:
[HelpURL("<https://www.youtube.com/channel/UCGjv_3tbzJ8yKuvacoqmO-Q>")]
public class AttributesDemoScript : MonoBehaviour
{
}
- Output:

- If you were to click this icon, a browser window would open taking you to the documentation for that specific component. It is very useful in team projects.
RequireComponent Attribute
- The RequireComponent attribute will assign a component automatically to the current game object.
- This attribute is useful when we know that that a script is going to be using specific components of the game object. Not only that, but it also prevents anyone from removing the component from the object.
- Syntax : [RequireComponent(typeof (Component))]
- Example:
**[RequireComponent(typeof (RigidBody))]**
- Remove the script and reattach this script component to the game object.
- Output :

- Once you try to delete the Rigidbody component from the object then it will give a warning like:

SelectionBase Attribute
- SelectionBase attribute prevents the frustration of selecting a child object when you meant to select the parent object.
- It is useful in big games in which the parent objects have many child objects.
- **Syntax : [**SelectionBase]
- Example:
[SelectionBase]
public class AttributesDemoScript : MonoBehaviour
{
}
- Output:

- When you try to click on the Child object , the Parent Object is selceted automatically as per above image because we attach a script with the SelectionBase attribute.
AddComponentMenu Attribute
- Using this attribute you will add a customize Component in Unity.
- You can find this a new component in the “Component Menu” on or “Add Component” in the Inspector Window.
- Syntax : [AddComponentMenu(“Menu name/Component name”)]
- Example:
[AddComponentMenu("My Menu/New Component")]
public class AttributesDemoScript : MonoBehaviour
{
}
- Output:

ExecuteInEditMode Attribute
- When ExecuteInEditMode attribute applied to a class the script it will perform whatever tasks it would normally do in play mode from the edit mode.
- Syntax : [ExecuteInEditMode]
- Example:
**[ExecuteInEditMode]**
public class AttributesDemoScript : MonoBehaviour
{
}

- Now add this new function which will change the scale of the Object.
void Start()
{
GetComponent<Transform>().localScale = new Vector3(2, 2, 2);
}
- You noticed that the script works without the project being in play mode.

Summary
Now you have seen the 15 different attributes, that you can use in your Unity projects. This will be extremely handy in so man cases. Especially, as soon as you work on bigger projects, or even work with other developers. Then having this little bit of extra controle and overview will make a huge difference.