Component.SendMessageUpwards

Switch to Manual
public void SendMessageUpwards (string methodName, SendMessageOptions options);
public void SendMessageUpwards (string methodName, object value= null, SendMessageOptions options= SendMessageOptions.RequireReceiver);

Parameters

methodName要调用的方法的名称。
value该方法的可选参数值。
options如果目标对象上不存在该方法,是否应报错?

Description

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

The receiving method can choose to ignore the argument by having zero arguments. If the options parameter 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 SendMessageUpwards("ApplyDamage", 5.0); }

// Every script attached to this game object and any ancestor // that has a ApplyDamage function will be called. void ApplyDamage(float damage) { print(damage); } }