Sisyphus_2019 2020-10-23 16:27 采纳率: 100%
浏览 267
已采纳

Unity 为什么调用Destroy(gameObject)后不自动执行onDestroy()方法?

代码如图:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Coin : MonoBehaviour {
    //追踪硬币的数量
    public static int CoinCount=0;

    // Use this for initialization
    void Awake () {
        //创建游戏对象,硬币数量加1
        ++Coin.CoinCount;
    }

    void OnTriggerEnter(Collider Col)
    //参数Col中存储在这次接触中进入碰撞体的物体信息
    {
        if (Col.CompareTag ("Player"))
            Destroy(gameObject);
    }

    void onDestroy()
    {
        Debug.Log("Object Destroed");
        //减少硬币数量
        --Coin.CoinCount;

        //检查剩余硬币数量
        if (Coin.CoinCount <= 0) {
            //胜利
            //摧毁计时器对象,启动庆祝焰火
            GameObject Timer=GameObject.Find("LevelTimer");
            Destroy (Timer);

            GameObject[] FireworkSystem = GameObject.FindGameObjectsWithTag ("Fire");
            foreach (GameObject Go in FireworkSystem)
                Go.GetComponent<ParticleSystem> ().Play ();
        }
    }
}

在我操作tag为player碰撞Coin后,在面板上Coin都消失了,但是并没有调用onDestroy方法,请问是为什么?

  • 写回答

1条回答 默认 最新

  • Jo.H 2020-10-26 18:10
    关注

    你试试吧onDestroy改成OnDestroy

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”