Messages are methods with specific names that Unity will automatically call when certain events occur. For example:
public class Example : MonoBehaviour
{
protected void Awake()
{
Debug.Log("Awake was called");
}
}
When an object with that component attached is created (such as when loading a scene or instantiating a copy of a prefab), Unity will automatically call that Awake
method because its name matches the MonoBehaviour.Awake
message.
Receiving a message does not use the override
Keyword like you would for a normal inherited method, you simply declare a method with the appropriate name and parameters (usually none, but check the documentation for the specific message you are receiving) as shown above.
Access Modifiers
Unity doesn't check the method's Access Modifier, which means you can make them non-public
to prevent other scripts from calling them while Unity can still call them properly.
If a class is not sealed
(meaning that is can be Inherited), then its messages should use the protected
Access Modifier. Using private
would mean that a child class can declare a message without knowing that the base class has the same one and Unity will only call the method in the child class, which almost always causes a but because the base class never receives a message it was expecting. So using protected
prevents other scripts from calling the method while ensuring that child classes are aware when they use the same message as their base class and if necessary the base method can be given the virtual
modifier for the child class to override
and call the base.Method();
.
If a class is sealed
(meaning that is can't be Inherited), then its messages should use the private
Access Modifier because using protected
would give compiler warning CS0628: new protected member declared in sealed class.
Common Messages
The MonoBehaviour
page in Unity's API documentation list all of the Messages they can receive, but here are the most common ones:
Message | Called |
---|---|
Awake |
When the object is first created, such as when loading a scene or instantiating a copy of a prefab. |
OnDestroy |
When the object is destroyed, such as when unloading a scene or destroying a single object. |
OnEnable |
Each time the object is enabled (after Awake ). |
OnDisable |
Each time the object is disabled (before OnDestroy ). |
Update |
Every frame while the object is enabled. |
LateUpdate |
Every frame while the object is enabled just like Update , but it happens later in the frame after the animation systems (such as Animancer) have updated. That means this method can be used to overwrite things controlled by animations before the frame actually gets rendered to the screen. |
FixedUpdate |
At a fixed rate which matches the physics system (50 times per second by default). This is good for physics interactions and other mechanics that need to be as consistent as possible rather than being related to the rendering frame rate. |
Reset |
When you first add the component (only in Edit Mode) and when you use the Reset function from its context menu. This is useful for setting up default values that can't be assigned by field initializers and can be useful for Component Referencing. |
OnValidate |
When the component is loaded or any of its values are modified in the Inspector (only in the Unity Editor). This is useful for ensuring that values are valid (such as preventing negative values where they don't make sense) and for Component Referencing. |
Note that when the above table refers to an object being enabled, this requires both the component in question to be Enabled as well as the GameObject
that it is attached to and all of its parent objects to be Active as well. For example, OnDisable
gets called with the component gets disabled or when its GameObject
gets deactivted.
ScriptableObject
s, EditorWindow
s, and various other classes can also receive certain messages.