C# Attributes

Attributes are a useful feature of C# which allows additional customization to be applied to variables and methods.

Attributes in C# and Unity:

C# has a large number of Attributes which can be applied to variables and methods. To do this, surround the Attribute keyword/s with square brackets (“[ ]”) prior to writing the variable/method.

Most Attributes are part of a namespace, such as “System” or “UnityEngine”.

Multiple attributes can also be applied to one variable or method.

Some common Attributes:

  • [Serializable]: Part of the System namespace. Forcefully marks a non-serialized class as serialized, so that all private and public fields are serialized by default.
  • [Obsolete]: Part of the System namespace. Marks a field as obsolete, indicating that it is outdated and usage should be avoided. Also adds a helpful tooltip that appears when referencing the obsolete field from another part of code.
  • [Flags]: Part of the System namespace. Indicates that an enumeration can be treated as a bit field; that is, a set of flags. In more understandable terms, it allows you to select/combine multiple enums from an enum list, instead of being limited to just one selected enum.

In my next few posts, I will be going into some more Unity-specific Attributes, and also how you can create your own Attributes to make some really useful effects in the Unity Inspector.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Chapter 5 : Extra Strength Methods

GET 66000 MINA Token with Participating on MINA Testnets

Better Together: Checkov and env0!

#8WeeksSQLChallenge Case Study #1 : Danny’s Dinner

How to manualy deploy static sites to Netlify without using SSGs.

Lab 4: Sensing: Force sensors and photocells

Java Integration with Amazon Cognito

Deposit and Withdraw PancakeSwap on Wollito.

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.

More from Medium

Unity C#: Classes & Structs

Interfaces vs Abstract Classes

Ascend to the next level with Higher Order functions

C# Refreasher: Dictionaries!