Destroy a gameobject after your preferred amount of seconds in Unity script (C#)

Multiple ways to do this but the way I did it was:

1. Created and attached the script to the object I wanted to destroy
2. In the script created a public gameObject
3. In the start put the destroy instructions = “Destroy(objectIWantedToDestroy,numberOfSeconds);”
4. In the editor dragged the object into the newly created public gameObject spot.

Example:

public gameObject objectIWantToDestroy;

{//Beginning of example class

start()
{//beginning of start function

Destroy(objectIWantToDestroy,4); //will be destroyed in 4 seconds
}//end of start function

}//End of example class

Link where I found the answer:https://answers.unity.com/questions/400517/shoot-script-question.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.