kemdo
kemdo

Reputation: 1459

unity yield waitforsecond not working

I am new in Unity developer, I start learning Unity with reading tutorials,demo,example, and video. And I have some trouble with use timer (like thread in unity, I guess it...), and here is my code:

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

I do not know why ....

Upvotes: 0

Views: 764

Answers (1)

kemdo
kemdo

Reputation: 1459

Yeah, I found the solution. Because i am going to Destroy(this.gameObject) before starting the coroutine. A destroyed object cannot keep running a coroutine. change Destroy(this.gameObject) after Destroy(Bum) solve the problem :3

Upvotes: 1

Related Questions