Unity Programming Tricks

When it comes to programming in Unity, there is a plethora of useful programming keywords and commands to improve in-editor workflow and speed up the development process.

Some of the keywords are dedicated to debugging, while others are designed to separate the editor logic from the scripting logic.

1. '[SerializeField]' Attribute

By default, Unity hides private variables from the inspector, but by adding a '[SerializeField]' attribute before them, they will become configurable in the inspector while keeping them inaccessible from within other scripts.

[SerializeField]
private int myPrivateVariable;

2. '[HideInInspector]' and '[System.NonSerialized]' Attributes

Both '[HideInInspector]' and '[System.NonSerialized]' attributes have a similar function, to hide public variables from the inspector, however, they slightly differ in terms of serialization.

The difference between '[HideInInspector]' and '[System.NonSerialized]' is that '[HideInInspector]' will merely hide the variable from the inspector, however, Unity will continue serializing it (meaning the default value could be different than the one provided in the code), '[System.NonSerialized]' on the other hand, will not only hide the variable from the inspector but will also signal Unity not to serialize it (meaning the default value will be reset between sessions to the one provided in the code).

[HideInInspector]
public int myInternalVariable = 0; //Will always be 0 after the first initialization, so if you decide to change the value in the script, it may not always reflect in the Editor. Often used to remove clutter from the inspector.

[System.NonSerialized]
public int myNonSerializedVariable = 1; //The initial value will always be 1, or whatever value is set. Often used to mimic a private variable, while keeping it accessible from within other scripts.

3. '[ExecuteAlways]' Attribute

'[ExecuteAlways]' forces the script to be executed in the edit mode and Prefab editing mode, even when the game is not playing. This can be useful for testing and debugging purposes.

The attribute is added before the class declaration:

[ExecuteAlways]
public class MyScript : MonoBehaviour {
    // ...
}

4. 'Debug.Break()'

'Debug.Break()' pauses the editor.

This function is useful when you want to check certain values in the inspector and you are not able to pause it manually.

void Update() {
    if (someCondition) {
        Debug.Break();
    }
}

5. '[Range]' Attribute

The '[Range]' attribute allows you to create a slider in the Editor for a public float or int variable, which can be useful for tweaking values on-the-go, in a more user-friendly manner. Just provide the min and max values, like this:

[Range(0f, 1f)]
public float myFloatVariable;

Comment below your favorite programming tips & tricks in Unity.

Suggested Articles
Unity List of Useful Keywords in C#
Introduction to Unity C# Scripting Language
Understanding Unity's Component-Based Architecture
Creating Interactive Objects in Unity
Implementing Kinetic Interactions in Unity
Opening Drawers and Cupboards with Specific Keys in Unity
Pick and Drop System Without Inventory in Unity