Been reading the Unity documents about writing scripts and this hit me.I don’t completely understand the sentence. Could anyone explain it in a plainer way please?
It is recommended that SendMessage() and BroadcastMessage() are used only for prototyping and that other functions are used wherever possible. For example, if we know which component we want to call a function on, we should reference the component directly and call the function that way. If we do not know which component we wish to call a function on, we could consider using Events or Delegates.
SendMessage looks at each component, searching for the function. It can get performance intensive, and you can be more direct than this.
Each componente has it’s functions, and you should reference than instead of using SendMessage. If you have a life script and want it to take damage, use life.damage(amount), instead of SendMessage(“damage”).
On prototyping, you just want to test stuff and performance isn’t much of a worry, so there it’s okay to use SM. But for advanced stages, don’t do it. Delegates are like function references, so if you can’t reference a component, you reference the function.
Don’t use SendMessage() or BroadcastMessage(), they are extremely slow and very painful to debug because you pass in a string. They are seldom used for quick prototyping to get something up and running but not ideal for a game release.
Instead, grab a reference to the script/component that holds the method you wish to call and use that reference to call whatever method you need from the script/component. scriptRef.MethodToCall();
Use an event or delegate if you need to call multiple scripts at once to access a method…