No not directly. SendMessage finds the function using reflection. You could however do something a bit clever and write your own version of SendMessage that looks for a catch all component on the target game object and calls that as well as sending the message normally.
I don’t know what language you are using, if you let me know then I will cobble together some script for you.
Edit: Code for MessageEx
Ok - so you would build a file called
#MessageEx.js
#pragma strict
interface MessageCatcher
{
function CatchMessage(message : String, parameter);
}
static class MessageEx
{
static function Send(target : GameObject, message : String, parameter)
{
Send(target.transform, message, parameter);
}
static function Send(target : Component, message : String)
{
Send(target, message, null);
}
static function Send(target : GameObject, message : String)
{
Send(target.transform, message, null);
}
static function Send(target : Component, message : String, parameter)
{
var catchers = target.GetComponents(MessageCatcher);
for(var catcher : MessageCatcher in catchers)
{
catcher.CatchMessage(message, parameter);
}
target.SendMessage(message, parameter, SendMessageOptions.DontRequireReceiver);
}
}
This makes you now send messages using
MessageEx.Send(someGameObjectOrComponent, "SomeMessage", optionalParameter);
The message gets sent as normal but anything implementing MessageCatcher can also get all of the messages. You might build an automatic broadcaster for instance:
#MessageBroadcaster.js
#pragma strict
class MessageBroadcaster extends MonoBehaviour implements MessageCatcher
{
function CatchMessage(message : String, parameter)
{
BroadcastMessage(message, parameter);
}
}
So if you add this to an object, any message sent to it with the MessageEx.Send will also be broadcast to all children.
Or an upwards router:
#MessageRouter.js
#pragma strict
class MessageRouter extends MonoBehaviour implements MessageCatcher
{
function CatchMessage(message : String, parameter)
{
SendMessageUpwards(message, parameter);
}
}
What do you mean by a 'message': Do you mean you want to be notified every time a message is sent to the gameObject containing the script via SendMessage, or perhaps every time a method is called on the script, or something else entirely?
– Julien-LyngeYes, I want a single function that will be called every time any message is sent to the script via the SendMessage function.
– ThePilgrim