Can someone tell me what this does precisely? What is the ‘receiver’ in this scenario?
I ask because it is in the code for the 2D sidescroller tutorial and features zero explanation there as well, along with the rest of the code.
While I must express my gratitude to the Unity team for providing such a high quality game engine at a reasonable cost, I can’t help but call them on their consistent shortcoming when it comes to explaining code, especially in their own API, but also in their so called ‘tutorials’.
when you use the ‘SendMessage’ function to send some kind of data to a function, you can set that option so you don’t get an error if the function your trying to call isn’t found.
It’s not their fault if you cant be bothered to use the materials provided:
“No receiver is required for SendMessage.”
Fairly simple. Don’t know what SendMessage is?
“This is used by SendMessage BroadcastMessage in GameObject Component.”
Ahh, so send message options can be used in multiple cases. Lets see what one of these are:
“Calls the method named methodName on every MonoBehaviour in this game object.”
// Calls the function ApplyDamage with a value of 5
SendMessage ("ApplyDamage", 5.0);
// Every script attached to the game object
// that has a ApplyDamage function will be called.
function ApplyDamage (damage : float) {
print (damage);
}
Cool, it allows me to remotely call a function on another component. I could use this, for example, to apply damage to an enemy!
While to you it might seem “fairly simple”, since I am new to scripting with Unity I make criticisms from my perspective as a learner. Like any good company, Unity will determine the validity (or lack thereof if the case may be) of customer feedback on issues such as ‘teaching’ or ‘information availability’ and use it to improve their company if indeed such a lapse exists.
I appreciate the links to the other information (I’m not being sarcastic, I really do), although I already knew what the SendMessage function itself was for (since I do indeed use the API reference frequently and respect the fact that it even exists at all), I simply wanted more specifics on DontRequireReceiver. And by the way, thanks to the rest of you for providing that information as best you can.
I understand that it will throw a error if not used. But this is true of a lot of things.
Yes, I surmised the basic premise of this code, I was looking for a more detailed explanation of why it was required at all. Why would you have a line of code whose only purpose is to prevent an error? Why would anyone write a SendMessage if the function in the first argument didn’t exist? And if it didn’t exist, wouldn’t you want the error code to alert you of such?
Here’s a straightforward practical example. Unity doesn’t have an OnDestroy() message; you can sort of work around this using OnDisable(), but another option is to create your own ‘destroy’ function that first sends an OnDestroy() message, and then destroys the object. The point is not to require that the target objects handle this message, but simply to give them the opportunity to; that’s why the ‘don’t require receiver’ option exists.
If you’re still wondering about the practical applications of this option, consider that (IINM, at least) pretty much all of the built-in Unity messages (Start(), Update(), LateUpdate(), etc.) don’t require a receiver.
Not in the above cases. You only want an error to be generated if it’s a requirement that the message is received, but the receiver is missing.
Seems (for me at least) that if you use SendMessageUpwards without SendMessageOptions.DontRequireReceiver in…let say the lower scripts , you`ll get the script to execute the function and for who know why…return an error after…
//script on child object function OnTriggerStay (other : Collider) {
if(other.tag ==“CCS”)*
{ *
cc =1;*
SendMessageUpwards (“GOMess”, 5.0);*
}* } //script on parent function GOMess(x) {print(x+“OK”);}