Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


SendMessage

Calls the method named methodName on every MonoBehaviour in this game object.
Read time 3 minutesLast updated 4 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

SendMessage(string, Object)

Calls the method named methodName on every MonoBehaviour in this game object.
public void SendMessage(string methodName, object value)

Parameters

methodName

Name of the method to call.

value

Optional parameter for the method.

Remarks

The receiving method can choose to ignore the argument by having zero arguments. If options is set to SendMessageOptions.RequireReceiver an error is printed when the message is not picked up by any component.
Note that messages will not be sent to inactive objects (ie, those that have been deactivated in the editor or with the GameObject.SetActive function).

Examples

using UnityEngine;public class Example : MonoBehaviour{ void Start() { /// Calls the function ApplyDamage with a value of 5 SendMessage("ApplyDamage", 5.0); } // Every script attached to the game object // that has a ApplyDamage function will be called. void ApplyDamage(float damage) { print(damage); }}

SendMessage(string)

Calls the method named methodName on every MonoBehaviour in this game object.
public void SendMessage(string methodName)

Parameters

methodName

Name of the method to call.

Remarks

The receiving method can choose to ignore the argument by having zero arguments. If options is set to SendMessageOptions.RequireReceiver an error is printed when the message is not picked up by any component.
Note that messages will not be sent to inactive objects (ie, those that have been deactivated in the editor or with the GameObject.SetActive function).

Examples

using UnityEngine;public class Example : MonoBehaviour{ void Start() { /// Calls the function ApplyDamage with a value of 5 SendMessage("ApplyDamage", 5.0); } // Every script attached to the game object // that has a ApplyDamage function will be called. void ApplyDamage(float damage) { print(damage); }}

SendMessage(string, Object, SendMessageOptions)

Calls the method named methodName on every MonoBehaviour in this game object.
public void SendMessage(string methodName, object value, SendMessageOptions options)

Parameters

methodName

Name of the method to call.

value

Optional parameter for the method.

Should an error be raised if the target object doesn't implement the method for the message?

Remarks

The receiving method can choose to ignore the argument by having zero arguments. If options is set to SendMessageOptions.RequireReceiver an error is printed when the message is not picked up by any component.
Note that messages will not be sent to inactive objects (ie, those that have been deactivated in the editor or with the GameObject.SetActive function).

Examples

using UnityEngine;public class Example : MonoBehaviour{ void Start() { /// Calls the function ApplyDamage with a value of 5 SendMessage("ApplyDamage", 5.0); } // Every script attached to the game object // that has a ApplyDamage function will be called. void ApplyDamage(float damage) { print(damage); }}

SendMessage(string, SendMessageOptions)

Calls the method named methodName on every MonoBehaviour in this game object.
public void SendMessage(string methodName, SendMessageOptions options)

Parameters

methodName

Name of the method to call.

Should an error be raised if the target object doesn't implement the method for the message?

Remarks

The receiving method can choose to ignore the argument by having zero arguments. If options is set to SendMessageOptions.RequireReceiver an error is printed when the message is not picked up by any component.
Note that messages will not be sent to inactive objects (ie, those that have been deactivated in the editor or with the GameObject.SetActive function).

Examples

using UnityEngine;public class Example : MonoBehaviour{ void Start() { /// Calls the function ApplyDamage with a value of 5 SendMessage("ApplyDamage", 5.0); } // Every script attached to the game object // that has a ApplyDamage function will be called. void ApplyDamage(float damage) { print(damage); }}