|
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.
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:
(comments are locked)
|
|
The only way I can see that wouldn't work is if the object you dragged onto the
(comments are locked)
|
|
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:
of course this could lead to calling all the functions with that name
hope this help anyone, cheers, 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)
|
|
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:
Hm, I've just seen Eric5h5's answer and he's probably right! :-) 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)
|
