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

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方法,请问是为什么?

你试试吧onDestroy改成OnDestroy