团结产量等待不起作用

时间:2014-10-16 11:32:17

标签: unity3d yield

我是Unity开发人员的新手,我开始通过阅读教程,演示,示例和视频来学习Unity。我在使用计时器时遇到了一些麻烦(比如统一线程,我猜它......),这是我的代码:

void OnCollisionEnter(Collision colli){

    if (colli.collider.name == "Car") {
        Debug.Log("On Collision naz ~.~");
        Destroy(Car);    
        Destroy(this.gameObject);
        GameObject clone = (GameObject) Instantiate(Bum, transform.position, Quaternion.identity);
        StartCoroutine(deleteObject(clone));
    // Just want to delete "clone" object after 1 second
    }
}

IEnumerator deleteObject(GameObject bum){
    Debug.Log("chuan bi destroy naz ~.~"); // <-- run normally
    yield return new  WaitForSeconds(1.0F);
    Debug.Log("Destroy rui naz =,='");    //  <-- not display
    Destroy (bum);
}

我不知道为什么......

1 个答案:

答案 0 :(得分:1)

是的,我找到了解决方案。因为我在开始协程之前要去Destroy(this.gameObject)。被破坏的对象无法继续运行协程。  在Destroy(Bum)解决问题后更改Destroy(this.gameObject):3