- Home /
SendMessage and BroadcastMessage not getting received outside the script.
I have a gameobject named Camerascript which contains two scripts, script1.cs and script2.cs
script2.cs has this line
BroadcastMessage("HandRaiseDetector_HandRaise", joint, SendMessageOptions.DontRequireReceiver);
script1.cs has this function
void HandRaiseDetector_HandRaise(ZigJointId zig)
{
Debug.Log("Hand Raised camera change");
}
but this function never gets called.However,if i place this function inside script2.cs it gets called. Same is the case if i use sendmessage instead of broadcast message why is the function not getting called in script1.cs.
A few thoughts/questions:
Are you positive that script1.cs is attached to the GameObject?
Is there a reason that there are two scripts; in other words, can you combine them and simply perform a method call?
Have you thought about using a Delegate and Event ins$$anonymous$$d of Send$$anonymous$$essage?
From what I'm reading of your architecture both Send$$anonymous$$essage() and Broadcase$$anonymous$$essage() should work; both scripts appear to be on the same GameObject. If that's the case then its very strange that it wouldn't work, and I'd file a bug report with Unity. But because it would be so strange I would first assume that you've configured something incorrectly with your scripts/gameObject.
I am 100% positive that script1.cs and script2.cs are attached to the same gameobject.
Yes, the reason is one of the script is from a library which performs just the function of sending various messages , i don't want to mix and mess it up.
No i haven't, but i will now. Thanks.
Do i need to provide a listener or something in scrpit1.cs for receiving the message, i just have the HandRaiseDetector_HandRaise function there.
The existing method would works as a handler; you just need to attach to it in Start/Awake and detach in OnDestroy or somewhere suitable to your needs.
I don't understand. How do i attach to it in Start. BTW delegate and event work as expected :), Thanks.
Sorry if I confused you. I'm typing on my phone so it's easy to make silly mistakes. What I meant was attach to the event on start or awake, but it seems like you got it working already so ignore that comment! :)