C# Unity Built-In Attributes (Part 1)

Today I’ll show you some really useful attributes that are built into Unity, which can have some nice effects on your code.

A few of the more common Attributes I use in Unity

Class Attributes:

Some attributes can be added to classes, rather than variables

There are some Attributes that can be added before the class definition in order to give the class certain properties or abilities.

There are many such Attributes, but the ones above do the following:

  • AddComponentMenu(): Allows you to override where in the Component Menu your script appears.
  • RequireComponent(): Forces a component of the type specified to be on the same GameObject as this script.

Variable Attributes:

Here are some Attributes that can be applied to variables in C# classes. There are many more from the UnityEngine and UnityEditor namespaces.

  • Header(): Adds a custom heading to that spot on the Inspector.
  • SerializeField: Makes a private variable show up on the Inspector as if it was public, while still being private.
  • HideInInspector: As the name suggests, this makes the variable not show up in the Inspector, regardless of its accessibility level.
  • Min(): Applied to number variables, it adds a minimum value to the Inspector, not allowing the value to go below this. There is no Max attribute.
  • Range(): Similar to Min, but adds both a minimum and a maximum value, while also adding a nice slider to the Inspector.

That’s all for today!

Since there is a lot of info and uses for Attributes in Unity, I will go over more of them in tomorrow’s post. I’ll be making a few posts about this, including how to make your own useful Attributes!



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
Vincent Taylor

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.