Is SendMessage immediate or not?

Just want to clarify something before I go about implementing it in code. Anyway, I’m setting up carriers that launch fighters at the player, then recall them if the player gets too far away. When a fighter gets recalled by a carrier, that carrier reserves space for the fighter.

However, if the fighter gets shot down while returning, I want the carrier to clear the reserved space. The current plan is for the HP-handling script on the fighter to send a message to the carrier-recall-handling script on the fighter when lethal damage is dealt, and then have the carrier-recall-handling script notify the carrier - all before the HP-handling script finishes up the method that sent the message, destroying the fighter.

As long as the sent message is handled immediately (before the method in the HP-handling script runs the rest of its code and destroys the fighter), there is no problem. However, if there’s a possibility that the sent message might get handled later, then it would wind up getting sent to a GameObject that no longer exists, and the carrier would not be notified.

Hence, the question: is SendMessage always processed completely, on the same line where it is called?

I don’t know how it would work otherwise, but here I go:

SendMessage uses reflection to find any methods on all components that are attached to the object SendMessage was called on. SendMessage goes not tie into the coroutine system, it’s pure and simple .NET reflection. It’s implementation looks roughly like this (pseudocode):

function SendMessage(name:String, argument:object) {
    //Get all components
    var components = this.GetComponents(Component);

    //Loop through all components
    for (var component in components) {
        //Get the type of the component (reflection)
        var type = component.GetType();

        //Get the method named <name> of the type
        var methodInfo = type.GetMethod(name);

        //Invoke the method on the components with the argument
        methodInfo.Invoke(component, [argument]);

As is made obvious by the code, SendMessage does not “delay” anything, so like all other pieces of code, they execute immediately (Yes, all code runs immediately (in the same thread), there might just be more code hidden behind which you haven’t realized)

You can really easily test this:

class Test extends MonoBehaviour {
    function Awake() {
        Debug.Log("Finish Test");
    function Test() {

That’s a couple lines of code, written in a couple seconds, which could have easily saved you the 5 minutes writing the question and the 10 hours of waiting for an answer :wink:

All code is always processed immediately, unless specifically stated otherwise in the docs (those few async functions that Unity has). Programming would never be able to work otherwise.