Ophélia
Ophélia

Reputation: 241

destroy a specific gameobject after an amount of time in unity

I kinda stuck with my script right now. When a gameObject, with my script attached to it, has a trigger event with a specific gameObject, I want to destroy the specific gameObject after an amount of time.

So i came to this:

void OnTriggerEnter ( Collider other) {

if (other.gameObject.tag == "leaf1"){
    StartCoroutine (LeafDestruction ());
    }
}

IEnumerator LeafDestruction(){

yield return new WaitForSeconds (5);
Destroy (gameObject);

}

I know it's a noob mistake but i think i miss something, because when i run this script, it destroys the gameObject with the script attached to it, and not the specific gameObject(with tags).

How can i fix that?

Upvotes: 3

Views: 10474

Answers (4)

Qaunain Meghjee
Qaunain Meghjee

Reputation: 31

Option 1

Add a float representing T (time) after the GameObject in the regular Destroy method

   if (collisionInfo.collider.tag == "Obstacle")
        { 
            Destroy(collisionInfo.gameObject, 2f);
        }

Option 2

Use the built-in Inoke method which lets you add a time delay as a float after calling in a method which it will execute

if (collisionInfo.collider.tag == "Obstacle") 
    {
      Invoke(nameof(PlayerDead),2f);
    }


        
void PlayerDead() 
    { 
        Destroy(collisionInfo.gameObject);
    }

Upvotes: 0

Hellium
Hellium

Reputation: 7356

A simpler solution is to use the optional 2nd parameter of the Destroy method:

The object obj is destroyed immediately after the current Update loop, or t seconds from now if a time is specified.

Given the official parameters:

Parameters
obj The object to destroy.
t The optional amount of time to delay before destroying the object.

You can adjust your if statement to:

if (other.gameObject.tag == "leaf1")
    Destroy(other.gameObject, 5.0f);

Upvotes: 9

zwcloud
zwcloud

Reputation: 4889

You destroyed the object instead of the leaf. The gameObject is an alias of this.gameObject, which is the game object this script component is attached to. Note MonoBehaviour inherits from Behaviour and Behaviour inherits from Component.

GameObject leafObject;

void OnTriggerEnter ( Collider other) {
    if (other.gameObject.tag == "leaf1"){
        leafObject = other.gameObject;
        StartCoroutine (LeafDestruction ());
    }
}

IEnumerator LeafDestruction(){
    yield return new WaitForSeconds (5);
    Destroy (leafObject);
}

Upvotes: 2

Michael Faisst
Michael Faisst

Reputation: 607

Basically you need to tell your coroutine that it should destroy the other.gameObject and not the gameObject that is running this script.

So what you could do is add a parameter to your coroutine, passing in the gameObject that it should really be destroyed:

void OnTriggerEnter ( Collider other) {

    if (other.gameObject.tag == "leaf1")
    {
        IEnumerator coroutine = LeafDestruction(other.gameObject);
        StartCoroutine (coroutine);
    }
}

IEnumerator LeafDestruction(GameObject toDestroy){
    yield return new WaitForSeconds (5);
    Destroy (toDestroy);
}

Upvotes: 2

Related Questions