BroadcastMessage(string, object, SendMessageOptions)
Calls the specified method on every MonoBehaviour attached to the GameObject or any of its children.
Read time 1 minuteLast updated 7 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
BroadcastMessage(string, Object, SendMessageOptions)
public void BroadcastMessage(string methodName, object parameter, SendMessageOptions options)
Parameters
The name of the MonoBehaviour method to call.
An optional parameter value to pass to the called method.
Whether an error should be raised if the method doesn't exist on the target object.
Remarks
A specified for a method that doesn't accept parameters is ignored. If is set to SendMessageOptions.RequireReceiver an error is printed when the message is not picked up by any component.
parameteroptionsusing UnityEngine;public class Example : MonoBehaviour{ void Start() { /// Calls the function ApplyDamage with a value of 5 /// // Every script attached to the GameObject and all its children // that has a ApplyDamage function will be called. gameObject.BroadcastMessage("ApplyDamage", 5.0); }}public class Example2 : MonoBehaviour{ public void ApplyDamage(float damage) { print(damage); }}
Additional Resources: MonoBeheaviour, GameObject.SendMessage, GameObject.SendMessageUpwards