BroadcastMessage X has no receiver!
Make sure you have declared the function in the correct scope.
Message functions cannot be called if they are local functions.
public class Example : MonoBehaviour
{
void Update()
{
// This is a local function.
//
Don't put your function inside of another.
void MessageFunction()
{
...
}
}
//
This is the correct scope for message functions.
void MessageFunction()
{
...
}
public void Broadcast() => gameObject.BroadcastMessage(nameof(MessageFunction));
}Using nameof makes it so your IDE can protect you against spelling mistakes.
If you do not have access to nameof because
public class Example : MonoBehaviour
{
//
Where possible, avoid using strings to refer to message functions.
public void Broadcast() => gameObject.BroadcastMessage("MessageFunction");
//
Use nameof, the function is in a scope this code can refer to by name.
public void Broadcast() => gameObject.BroadcastMessage(nameof(MessageFunction));
void MessageFunction() { ... }
}You can pass SendMessageOptions.DontRequireReceiver to BroadcastMessage so it won't print an error when there is no receiver present on the target.
gameObject.BroadcastMessage(nameof(MessageFunction), SendMessageOptions.DontRequireReceiver);BroadcastMessage is an expensive and brittle way to send messages between objects.
Consider using the GetComponents overload that takes a list (avoiding allocating a new array), getting an interface that defines your message function. Then loop over the results, calling your function.