Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CraigCrawford · Jan 06, 2014 at 08:30 PM ·

How to Destory a Gameobject in C# after 3 seconds?

I'm trying to destroy a explosion after three seconds but my method is not working. I make the explosion after I kill the enemy in the EnemyAI script. I find the script attached to the explosion and call the function in the script to destroy the game object.

EnemyAI Script

 public class EnemyAI : MonoBehaviour {
 
 public GameObject explosion;
 
 void Death(){
 
 Destroy (gameObject); //kill the enemy
 
 Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
         
 
 Instantiate(explosion, transform.position, randomRotation);
 
 explosion.gameObject.GetComponent<DestroyItems>().DestroyExplosion();
  
 }
 }

DestroyItems Script

 public class DestroyItems : MonoBehaviour {
 
     float lifetime = 3.0f;
 
     public void DestroyExplosion ()
     { Destroy (gameObject, lifetime); }
 }
 

There are no errors, but the explosion does not disappear. I get a warning saying to use DestroyImmediate(theObject, true) which I don't want to use because it will cause problems later with missing assets in game folder.

Please avoid using IEnumerator and Coroutine in your answers. I'm just looking for something simple and I don't need complex answers because I know there must be a easy way to do this. Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Socterean · Jan 06, 2014 at 08:48 PM

 using UnityEngine;
 using System.Collections;
 
 public class DestroyByTime : MonoBehaviour
 {
     public float lifetime;
 
     void Start ()
     {
         Destroy (gameObject, lifetime);
     }
 }

This is what you need. For better understandig of how and why I suggest watching this video that will explain all you need to know to overcome youre problem:

  • DestroyByTime

NOTE: under the video you will see the script that I shown you here.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Pemu · Jan 06, 2014 at 08:47 PM

Use SendMessage is very helpful.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by CraigCrawford · Jan 07, 2014 at 06:19 PM

Thanks Socterean this video is one of the best tutorials I have seen. I can actually learn from it because I don't get lost in unnecessary stuff that sometimes occurs in tutorials. I guess IEnumerator and Coroutine is not as complex as I thought well at least the way this video shows how to do it. Thanks Again!!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detect mouse click coordinates on terrain in C# 2 Answers

How do I get a line renderer to draw over sprites? 2 Answers

How do i make so it is two players on the same computer? (double screen) 2 Answers

Publishing without a license 0 Answers

Quiz game using unity or ngui 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges