LanguageEnglish
  • C#

GameObject.BroadcastMessage

Switch to Manual

Declaration

public void BroadcastMessage(string methodName, object parameter = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

Parameters

methodName The name of the MonoBehaviour method to call.
parameter 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 or any of its children.

A parameter specified for a method that doesn't accept parameters is ignored. If options is set to SendMessageOptions.RequireReceiver an error is printed when the message is not picked up by any component.

using UnityEngine;

public class Example : MonoBehaviour { void Start() { /// Calls the function ApplyDamage with a value of 5 /// // Every script attached to the GameObject and all its children // that has a ApplyDamage function will be called. gameObject.BroadcastMessage("ApplyDamage", 5.0); } }

public class Example2 : MonoBehaviour { public void ApplyDamage(float damage) { print(damage); } }

Additional resources: MonoBeheaviour, GameObject.SendMessage, GameObject.SendMessageUpwards


对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961