Unity QuickTip: Designer Variables

Hey everyone. I wanted to share a quick tip that I've been using in Unity to keep my class integrity intact, make my code more readable and expose it to the inspector. I call them designer variables and declare them like this:

[SerializeField, Tooltip("This tooltip will show in the editor.")]
private float _fooNumber;// This will display as Foo Number in the inspector.

The "tip" is combining the SerializeField and Tooltip attributes to prevent extra vertical line usage and declaring the variable as private with the _ in front.

It's a simple thing but I don't see it often and I've found that it makes code a lot easier to read/understand. This is especially true when working in teams. It immediately communicates that the property is expected to be changed in the inspector. It also allows designers to get a hint as to what the variable changes while in the inspector view.

There is a tertiary effect of not providing intellisense that you would normally get with tags. This can be a pro or con depending on the usage, but you can still add /// Property descriptions. without any problems.

Some links to learn more about attributes
Tutorials Point - C# Attributes
Microsoft - C# Attributes
List of Unity attributes (Found in the left hand pane)


  • To add to the editor vars inspector organisation:

    Use [Header("abc")] to group your inspector vars under ..u guessed it .. a header

    so Something like ..

    [Header("Gun Attributes")]
    public float GunMass = 0.25f; //Used for default speed reduction
    public float FireSpeed = 0.25f;
    public bool CanLook = true;

    to limit numbers you can use ....

    public float UnitSpeedReduction = 0f;

    will add a slider to the inspector making sure that engineers cant assign a value outside of the range
    Thanked by 1Rigormortis
  • +1 to this because using [SerializeField] instead of just making your fields public means that field accessibility isn't being dictated by whether you want something exposed to the editor. Other classes shouldn't have access to fields by default just because you want design time access to them. Sorry, pet peeve of mine in usual Unity code :P
  • Learnt about this recently too and it can be super useful - can call a function in your script by right clicking the component and finding the function at the bottom if you use [ContextMenu(" ")].

    Can also be helpful for designer doing stuff
    /// Add a context menu named "Do Something" in the inspector
    /// of the attached script.
    [ContextMenu("Do Something")]
    void DoSomething()
      	Debug.Log("Perform operation");

    There's also a hacked together button attribute you can use to make a inspector button out of a functino instead of always making a custom inspector to do it.

    436 x 149 - 7K
    Thanked by 1Elyaradine
  • I didn't originally intend for this to be about attributes, but since people seem to want to learn/discuss more about it here are some helpful links. :)

    Tutorials Point - C# Attributes
    Microsoft - C# Attributes
    List of Unity attributes (Found in the left hand pane)
Sign In or Register to comment.