Can someone explain SendMessage to me?

I have two objects in a scene, one called Input Manager and the other called Indicator Manager. on Input Manager, I have the following:

//TiltManager.js - Acts as a message controller for all tilt events

private var horizontalOrientation : boolean = true;
private var accelerator : Vector3 = iPhoneInput.acceleration;

static var turn : float;
static var tilt : float;

var receiver : GameObject;
var sensitivity : float = 1.0;
var tiltDeadStick : int = 0;
var zeroAngle : int = 0;

function Awake () {

if (horizontalOrientation)
Screen.SetResolution(480, 320, true);
Screen.SetResolution(320, 480, true);

    } //end Awake

function Update () {

    turn = ((iPhoneInput.acceleration.z * -1) * sensitivity);
    tilt = ((iPhoneInput.acceleration.x - zeroAngle) * sensitivity);

if (turn > tiltDeadStick){
receiver.gameObject.SendMessage ("TiltRight");
} //end if

if (turn < -(tiltDeadStick)){
receiver.gameObject.SendMessage ("TiltLeft");
} //end if

if (tilt > tiltDeadStick){
receiver.gameObject.SendMessage ("TiltForward");
} //end if

if (tilt < -(tiltDeadStick)){
receiver.gameObject.SendMessage ("TiltBack");
} //end if  

} //end Update

I set the `receiver` to the Indicator Manager object. My understanding is that doing this will allow me to send a message from the Tilt Manager (ex. if I tilt the iPhone forward, sends a message "TiltForward") to Indicator Manager, which will then look for a method (function) called TiltForward and execute the contents of that method. This seems to be working so far.

How do I make is so I don't need a specific receiver? I would like to have objects 'subscribe' to events and listen for them; this way I can have multiple objects listen for say a "TiltForward" message and execute different code when they get it. (Ex. Indicator Manager gets the message, fades a graphic in on the screen, while the player GO gets the message and rotates on the X axis along with the movement) As a note, I have looked at the messaging stuff on the wiki; I am looking for an easier to configure solution. Can I just use a GO array for the receiver? Would Unity then iterate through the objects in the array, sending the message to each one? Is this efficient at all or terribly slow?

To answer the title question: SendMessage is a means of remotely calling some other game object’s method. It’s similar in concept to a RPC, only a Message doesn’t occur across a network, just on a single agent. The OP’s understanding of what a message is is 100% correct.

To answer the actual question, messaging appears to be the wrong approach; the problem of phone tilt causing specific objects to tilt in a specific way sounds like a common behavior that should be attached to all of a certain object, that listens to the Input class for the individual object’s behavior. As other posters have noted, Messaging in Unity is known to be slow; having your game objects be associated with a prefab that has this input-listening behavior attached, or programmatically adding the input-listening behavior on object creation are usually going to be significantly more performant and efficient means of getting these objects to respond to mechanical inputs.

For a given object to use a different piece of code on event receipt, I highly recommend using A) C#, and B) a little bit of inheritance to do it, as such:

Note: The below is untested code, as I am writing this at work! This setup may required tweaking to be workable.

Sensor Script

public class TiltSensor : MonoBehaviour
    #region Variables / Properties

    private TiltResponse _response;

    #endregion Variables / Properties

    #region Engine Hooks

    public void Start() 
        _response = GetComponent<TiltResponse>();

    public void FixedUpdate()
        TiltType turn = DetermineTiltType();
        if(turn > TiltDeadStick)

        // SNIP: Other cases/actions...

    private DetermineTiltType() 
        // STUB, Bring your own implementation!!

    #endregion Engine Hooks

TiltResponse Base Class:

public abstract class TiltResponse : MonoBehaviour
    public abstract void OnRightTilt();
    public abstract void OnLeftTilt();
    public abstract void OnUpwardTilt();
    public abstract void OnDownwardTilt();

Specific behavior:

public class FooBehavior : TiltResponse
    public override void OnTiltRight()
        // Specific actions go here...

    // ...Implement other abstract methods as necessary...

    // ...and because the base inherits from MonoBehaviour, you can
    // still override Start(), FixedUpdate(), Collision/Trigger methods, etc.

This way, objects of a particular type all follow a specific set of rules that is implemented in one place in code, but could be programmatically attached to some newly-spawned prefab if that’s the way you’re going. As long as a component that fulfills the interface that consists of those tilt checks and the MonoBehaviour interface is on the same game object, you’re golden.

You can use SendMessageOptions.DontRequireReceiver as the last parameter of your SendMessage to allow sending to an object without a receiver

SendMessage however is pretty slow (in the area of 250 times slower than a normal function call) - i would recommend against using it for an event system if possible

Your idea about a GameObject array however would work, though again, not the fastest of implementations

Using SendMessage often is definitely not the fastest method out there. Rather than keeping an array of GameObjects, you can keep an array of functions that all have the same signature.

enum TiltType

var listeners : Array = new Array ();

function AddListener (listener : Function)
    listeners.Add (listener);

function Update ()
    // ... Your tilt detection
    var tiltType : TiltType;

    if (turn > tiltDeadStick)
        tiltType = TiltType.RIGHT;
    // ... other cases

    for (var listener : Function in listeners)
        listener (tiltType);

function HandleTilt (tiltType : TiltType)
    Debug.Log ("Recieved tilt: " + tiltType);

You see here that you can pass whatever function you'd like as long as it takes a TiltType as the argument. Then the function decides whether or not and how to handle the message.