2014-10-16 50 views
0

我是Unity開發人員的新成員,我開始學習閱讀教程,演示,示例和視頻的Unity。和我有一些麻煩,使用定時器(如在團結線,我想這...),這裏是我的代碼:統一產量waitfreescond不工作

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); 
} 

我不知道爲什麼....

+0

被燒燬了嗎?我在問,因爲我在實踐中對產量報表感到困惑。如果交換Debug.log()和destroy語句,是否顯示文本? – MVCDS 2014-10-16 11:55:25

+0

是的。 debug.log()後,銷燬語句不運行,我認爲產量不運行:( – kemdo 2014-10-16 12:13:04

+0

產量WaitForSeconds(1)導致問題,需要yeild retun新waitforsecond(); – kemdo 2014-10-16 13:05:18

回答

1

呀,我找到了解決方案。因爲我要在啓動協程之前去銷燬(this.gameObject)。被破壞的對象無法繼續運行協程。 更改Destroy(this.gameObject)之後Destroy(Bum)解決問題:3