public void SendMessage (string methodName);
public void SendMessage (string methodName, object value);
public void SendMessage (string methodName, object value, SendMessageOptions options);
public void SendMessage (string methodName, SendMessageOptions options);

Parameters

methodName要调用的方法的名称。
value该方法的可选参数。
options如果目标对象没有为消息实现该方法,是否应报错?

Description

调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。

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.

注意,不会将消息发送到非活动对象(即,在 Editor 中或使用 GameObject.SetActive 函数已停用的对象)。

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); } }