Search Results for

    Show / Hide Table of Contents

    Sending Messages via EMO Reference

    Using EditorMessageObjects (EMOs) to send Messages gives you the most flexibility and control at the same time. This workflow allows you to send Messages from custom scripts while the MessageType is selected from the Editor. This enables you to write scripts in a general way and set up the sent MessageType later in the Editor according to the specific context.


    To use an EMO in your script, you have to expose a field of its type or a base type to the Editor. This can be done by declaring it as public or using the SerializeReference attribute on a private property.

    Note

    In most cases you want your exposed property to be of the base types MessageType or MessageType<T>. This way, you can select any of the derived MessageTypes in the Editor, giving you the most flexibility.

    To send the Message using the EMO from your script, call Send on your property.

    The following code sends the Message whenever the spacebar is pressed down:

    • Without Parameter
    • With Parameter
    using MalteHusung.GlobalMessage;
    using UnityEngine;
    
    public class SendingViaEmo : MonoBehaviour
    {
        public MessageType MessageToSend;
    
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                MessageToSend.Send(this);
            }
        }
    }
    
    using MalteHusung.GlobalMessage;
    using UnityEngine;
    
    public class SendingViaEmoWithParam : MonoBehaviour
    {
        public MessageType<int> IntMessageToSend;
        private int _pressCounter = 0;
        
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _pressCounter++;
                IntMessageToSend.Send(_pressCounter, this);
            }
        }
    }
    

    Don't forget to assign your EMO in the inspector.

    Inspector with assigned EMO

    In This Article
    Back to top Malte HusungLeave a ReviewLegal Noticethank you ❤️