- Home /
SendMessage within a SendMessage Receiver
Hello folks,
Having a bit of a strange issue regarding SendMessage usage. What is happening is there are two classes that bounce messages back and forth. The first class has an array of gameobjects that have the second class, and it cycles through those as messages are returned. When I try to send a return message within the receiver, the receiver ends up being called twice! Here is my code flow:
Class1 sends a message.
Party[CurrentPartyMember].SendMessage ("PromptForCommands");
Class2 has function "PromptForCommands", which enables some GUI options. It then has this function to return a message.
void GetCommand ()
{
if (Input.GetKeyDown ("x"))
{
SendMessageUpwards ("CommandSelected", CommandList[CommandSelected]);
CommandListActive = false;
}
}
Class1 is the parent of this gameobject. It receives with this class:
void CommandSelected (string command)
{
Debug.Log (CurrentPartyMember);
Commands[CurrentPartyMember] = command;
CurrentPartyMember++;
Party[CurrentPartyMember].SendMessage ("PromptForCommands");
}
And the cycle continues (waiting for input). The odd thing that happens here, is if you look at the debug after one 'cycle', you see this!
0
1
Instead of there just being one Debug, there is two. However, if I move the SendMessage from my "CommandSelected" function and move it to "Update"...
0
So clearly there is some issue with sending a message within my receiver. I have put a debug in the "GetCommand" function which returns the message, but it is only ever executed once. So my question is how is my "CommandSelected" happening twice? I'm not telling it to execute anywhere.
I can solve this by putting the code into my Update instead, but then I am calling the SendMessage every frame as opposed to just when it needs to be called. If anyone has some insight on this it would be very much appreciated!