I love this post from stack overflow (I think it should still be open, because it showed me a lot of little known attributes), and I notice that I don´t use attributes very much in Unity, although they seem very powerful. Which are the most useful ones, or at least the ones that you use more often?
A list of all Unity attributes can be found here:
The Attributes I use most are Serializable to display whole objects in the inspector
[System.Serializable]
class MyModel
{
public int Id;
public Color C = Color.white;
}
.
HideInInspector if I have public fields and don’t need the accessor
[HideInInspector]
public int MyCounter;
.
And RequireComponent to prevent null pointer exceptions
[RequireComponent(typeof(Rigidbody))]
public class MyClass : MonoBehaviour
{ ...
Great question! Though perhaps better suited for the forum because of the discussion/debate nature of the question and answers which is sure to follow. There is no single answer for this, but the attribute I use most often is [SerializeField], for example:
[SerializeField]
private Transform prefabPrototype;
This attribute causes a field to appear in the inspector where you can manipulate it. I use it because Unity only serializes public variables by default, not private ones. But in object oriented programming, there are lots of cases where it makes no sense to make a variable public, and I refuse to violate that principle just to work with the variable in the editor. [SerializeField] solves that.
For me it’s no contest…
[RPC]
All the others I could do without.