x


SendMessage StartSending has no receiver!

I am trying to call a method of a script attached to a different object. I have the following code, and I've attached the object to the localPlayerObject variable in the inspector, but I am still getting an error saying there is no receiver.

public Transform localPlayerObject;

private void SpawnLocalPlayer() {
    localPlayerObject.SendMessage("StartSending");
}

I tried Debug.Log(localPlayerObject.name); and it gave me the correct name, so I know I am referencing it correctly. And yes, StartSending is declared in the other script:

void StartSending() {
    sendMode = true;
}
more ▼

asked Apr 13 '10 at 08:57 PM

James Simpson gravatar image

James Simpson
403 14 15 23

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

The only way I can see that wouldn't work is if the object you dragged onto the localPlayerObject variable was a prefab instead of an instance of a prefab.

more ▼

answered Apr 13 '10 at 09:11 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

(comments are locked)
10|3000 characters needed characters left

This probably is no necessary any more but here is what I found, having the same problem.

Theory: To send a message you receiver should be in the game object as the one you are sending it. It would look useless but the whole idea is to add scripts to an object without bothering about references, so different script doing different things having a function with the same name can be attached and remove to the same object on different times during runtime. Component.SendMessage

The problem: Since you are just sending a message but your receiver is in another game object, its impossible for the message to be receive across game objects.

Solutions:

  • having a reference to the object with function you want to send the message to and calling its function directly (as SendMessage would be pointless)
  • having a father/root in common, taking its reference and using BroadcastMessage function to call the function, kind of like:

transform.root.BroadcastMessage("myFunction")

of course this could lead to calling all the functions with that name

  • sort your game objects by purpose nesting into empty GameObjects, having a manager to reference the head of each group and call BroadcastMessage on each head
  • and... well, thats all that came to my mind, I used the first solution since it blend well with my problem.

hope this help anyone,

cheers,

more ▼

answered Jun 16 '12 at 07:32 PM

y0ux gravatar image

y0ux
0

First this question is 2 years old and has already been answered...

Second, Your theory is completely wrong :D Since you call the SendMessage function of another object, the SendMessage function invokes the function on it's attached MonoBehaviours. Since the function belongs to the target object (or a component on that object) it doesn't matter who calls the function.

Jun 16 '12 at 07:40 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

As far as I can see, what you have - as written - should work. This means the problem must be somewhere else, presumably in how you have your objects and scripts set up in the hierarchy.

Make sure:

  • You aren't mixing script types (c# and JS)
  • You have the script containing the "StartSending" method attached to the "localPlayerObject" (not one of its children)

Hm, I've just seen Eric5h5's answer and he's probably right! :-)

more ▼

answered Apr 13 '10 at 09:15 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

Actually, mixing script types is a perfect reason to use SendMessage, as opposed to other ways like GetComponent. SendMessage is totally runtime so it doesn't depend on compile order at all.

Apr 13 '10 at 10:26 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5059
x1940
x184

asked: Apr 13 '10 at 08:57 PM

Seen: 6552 times

Last Updated: Jun 16 '12 at 07:40 PM