Search Results for

    Show / Hide Table of Contents

    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:

    • Without Parameter
    • With Parameter
    using UnityEngine;
    
    public class SendingViaCodeOnly : MonoBehaviour
    {
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                MyNewMessage.Send<MyNewMessage>(this);
            }
        }
    }
    
    using UnityEngine;
    
    public class SendingViaCodeOnlyWithParam : MonoBehaviour
    {
        private int _pressCounter = 0;
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _pressCounter++;
                MyNewIntMessageType.Send<MyNewIntMessageType>(_pressCounter, this);
            }
        }
    }
    
    In This Article
    Back to top Malte HusungLeave a ReviewLegal Noticethank you ❤️