Sending Messages via Code Only
Sending Messages via code only gives you the most control, as Editor users cannot interfere. At the same time, your scripts are less flexible because the sent MessageType is “hard coded” and cannot be changed easily from the Editor. However, this workflow can still be very useful if you do not need the flexibility or cannot use editor features e.g., in Editor scripting.
To send Messages, call the Send
method on your MessageType.
The following code sends a Message whenever the spacebar is pressed:
using UnityEngine;
public class SendingViaCodeOnly : MonoBehaviour
{
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
MyNewMessage.Send<MyNewMessage>(this);
}
}
}