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:

  1. Header
  2. Space
  3. Tooltip
  4. Range
  5. TextArea
  6. Multiline
  7. SerializeField
  8. HideInInspector
  9. ContextMenuItem
  10. ContextMenu
  11. HelpURL
  12. RequireComponent
  13. SelectionBase
  14. AddComponentMenu
  15. 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.
New Unity Project
  • This is our Unity IDE.
Unity IDE

Create a Scene with 3D Object

  • Add a Sphere – 3D Object in the scene.
New Sphere Object
  • Add 1 capsule as child of the sphere object as shown in the image below.
Capsule in Sphere Object

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”.
Create a new C# Script
  • 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.


Header Attribute in Unity3D

  • This attribute is useful by adding a header above fields in the Inspector.
  • Syntax : [Header (“String”)]
  • Example:
  • Output:
Header Attribute
header attribute 2

Space Attribute

  • This attribute is creating a space between fields.
  • Syntax : [Space (“int”)]
  • Whereby the value you pass is the padding in pixels
  • Example:
  • Output:
Space attribute

Tooltip Attribute

  • This attribute creates a tooltip, that appears once you hover over the variable with the attribute
  • Syntax : [Tooltip (“text”)]
  • Example:
  • Output:
ToolTip Attribute Demo

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:
  • Output:
Range Attribute Demo

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:
  • Output:
TextArea Attribute
  • 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:
  • Output:
Multiline Attribute

SerializeField Attribute

  • The SerializeField attribute is used to show private variables in the inspector.
  • **Syntax : [**SerializeField ]
  • Example:
  • Output:
SerializeField Attribute

HideInInspector Attribute

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

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:
  • 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:
  • 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:
  • Output:
HelpURL Attribute
  • 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:
  • 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:
Cant Remove Object Warning

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:
  • Output:
SelectionBase Attribute
  • 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:
  • 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:
  • Now add this new function which will change the scale of the Object.
  • You noticed that the script works without the project being in play mode.
Sphere Scale Automatic change via script


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.