SendMessage(string, object, SendMessageOptions)
Calls the specified method on every MonoBehaviour attached to the GameObject.
Read time 1 minuteLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
SendMessage(string, Object, SendMessageOptions)
public void SendMessage(string methodName, object value, 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 parameter specified for a method that doesn't accept parameters is ignored. If is set to SendMessageOptions.RequireReceiver an error is printed if the message is not picked up by any component.
valueoptionsNote: Messages are not sent to MonoBehaviours attached to objects for which GameObject.activeSelf or GameObject.activeInHierarchy are .
falseusing UnityEngine;public class Example : MonoBehaviour{ void Start() { // Calls the function ApplyDamage with a value of 5 // Every script attached to the GameObject // that has an ApplyDamage function will be called. gameObject.SendMessage("ApplyDamage", 5.0); }}public class Example2 : MonoBehaviour{ public void ApplyDamage(float damage) { print(damage); }}
Additional Resources: MonoBehaviour, GameObject.SendMessageUpwards