前文:https://blog.csdn.net/Jaihk662/article/details/86766641(Invoke计时函数与碰撞销毁)
一、SendMessage消息发送
gameObject.SendMessage(string, value, SendMessageOptions):通知游戏物体gameObject身上的脚本文件执行它的方法string,value为传递的内容,可为空,options为信息接收方式,留空默认为SendMessageOpions.RequireReciver
一个简单的应用如下:每当主角收获一枚Coin,Coin的转速降低当前的1%
Coin自身的脚本(用于销毁和转动):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinDest: MonoBehaviour
{
static float sp = 10;
private Transform myTransform;
void Start()
{
myTransform = gameObject.GetComponent<Transform>();
GameObject.Destroy(gameObject, 8);
}
void Update()
{
myTransform.Rotate(new Vector3(0,0,1), sp);
}
void Slower()
{
sp = sp * 0.99f;
}
}
主角部分脚本(用于触发Coin的销毁):
void OnTriggerEnter(Collider coll)
{
if(coll.gameObject.tag=="Coin") //如果是和金币发生碰撞
{
coll.SendMessage("Slower"); //让金币转速变慢
GameObject.Destroy(coll.gameObject); //销毁金币
}
}