[C#] Getting a return from SendMessage?

I'm trying to work out if there is any way to get a return value from a function call besides instantiating a copy of the script like this:

MonoName variable = (MonoName)Object.GetComponent("MonoName");

I need to be pulling many values at different times from many different similar objects (an ever changing number of them), and could really do with a simpler way. I tried using:

Object.SendMessage("function", out variable);

with the following being in a script attached to Object:

int function(out variable)

but to no avail.

I apologise if the question seems poorly worded, explaining things isn't exactly a strong point of mine :P

While it's true you can't technically get a return value from functions called via SendMessage, you can still return data to the caller of SendMessage. Just use a class in as the data passed in as the 2nd parameter. The receiver of the message can modify the members of the class, and then the script that called SendMessage can use the contents of the class when SendMessage returns.

Here's an example....

Script #1 (MessageArgs.cs - the class to pass between them)

public class MessageArgs {
    public MessageArgs() {
    }

    public bool doSomething = false;
}

Script #2 (the sender)

void Start() {
    MessageArgs args = new MessageArgs();
    SendMessage("TestMessage", args);
    if(args.doSomething) {
        // do it!
    }
}

Script #3 (the receiver)

void TestMessage(MessageArgs args) {
    args.doSomething = true;
}

No, it's not possible to get a return value from SendMessage or BroadcastMessage, they're just basic functions (I personally don't even use them anymore, they're too basic, and typically not very fast).

Perhaps you should think about creating a class that manages the various script references that you have, and can pass out references to objects, if what you've created is actually that complex. You could have a class register/unregister with this object, and then other objects could request an instance of the object from its master list. This method would probably a lot faster than using GetComponent.

That, or make the methods you want to call static (if the class is unique, that is), so you don't need its instance, you can just call the static member directly.