Unity - Scripting API: GameObject.SendMessage
Success!
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Submission failed
For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Declaration
public void SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);
Parameters
| Parameter | Description |
|---|---|
| methodName | The name of the MonoBehaviour method to call. |
| value | An optional parameter value to pass to the called method. |
| options | Whether an error should be raised if the method doesn't exist on the target object. |
Description
Calls the specified method on every MonoBehaviour attached to the GameObject.
using 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); } }