unity中SendMessage的bug – unity3d游戏开发

SendMessage在unity中可以非常方便的调用到物体上脚本的方法但是也存在一个隐藏的bug.看下面代码:

发送者

1 public class testSender : MonoBehaviour {
2     public GameObject target;
3      
4     void OnGUI () {
5         if(GUILayout.Button("send",GUILayout.Width(200),GUILayout.Height(100))){
6             target.SendMessage("Test",null);
7         }
8     }
9 }

接收者

1 public class testReceiver : MonoBehaviour {
2      
3      
4     void Test (GameObject g) {
5         Debug.Log("receive "+g);
6     }
7 }

上面代码中接收者方法中需要一个GameObject类型的参数,那么发送者的SendMessage必须要添加一个参数,但是当参数为null时候,unity就会报错:

1 Failed to call function Test of class testReceiver
2 Calling function Test with no parameters but the function requires 1.

SendMessage的时候unity会过滤掉为空的参数,这样你实际传过去是没有带参数的,但是这个过滤却显得很愚蠢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值