Version: 2021.2
言語: 日本語
public void SendMessage (string methodName, object value= null, SendMessageOptions options= SendMessageOptions.RequireReceiver);

パラメーター

methodName 呼び出すメソッドの名前
value 呼び出すメソッドに渡す値
options ターゲットのオブジェクトにメソッドが存在しない場合、エラーを発生させるかどうか

説明

ゲームオブジェクトにアタッチされているすべての MonoBehaviour にある methodName と名付けたメソッドを呼び出します

The receiving method can choose to ignore the argument by having zero parameters. If options is set to SendMessageOptions.RequireReceiver an error is printed if the message is not picked up by any component.

このメッセージは非アクティブのゲームオブジェクトには送信されないことに気をつけてください(例えば、エディターのインスペクターや SetActive によって非アクティブにしている場合です)

using UnityEngine;

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

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