1usingSystem.Collections;2usingSystem.Collections.Generic;3usingUnityEngine;45publicclassPlayer:MonoBehaviour{6Rigidbody rb;// テスト用:キャラ動かすため7publicList<Collider> myNotifyObjects =newList<Collider>();// 自身の範囲内にあるトリガー対象89publicGameObject test;// テスト用:範囲内でオブジェクトを消すテスト用1011// Start is called before the first frame update12voidStart(){13 rb =GetComponent<Rigidbody>();14}1516// Update is called once per frame17voidUpdate(){18// テスト用:キャラ動かして実験用19 rb.velocity =newVector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw("Vertical"));2021// テスト用:スペースキーを押して設定したオブジェクトをDestroyしてみる22if(Input.GetKeyDown(KeyCode.Space)){23Destroy(test);24}25}2627privatevoidOnTriggerStay(Collider other){28if(!myNotifyObjects.Contains(other)){29 myNotifyObjects.Add(other);3031var c = other.GetComponent<NotifyObject>();32if(c ==null) c = other.gameObject.AddComponent<NotifyObject>();3334 c.OnDestroyListener +=()=>OnTriggerExit(other);35}36}3738privatevoidOnTriggerExit(Collider other){39Destroy(other.gameObject.GetComponent<NotifyObject>());40 myNotifyObjects.Remove(other);41}4243}44