Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by PumaManMan · Mar 08, 2014 at 11:09 PM · instantiatedestroyprefabs

How to destroy an instantiated prefab in C#?

I'm triggering an animation that plays when the Enemy mesh is colliding with a Projectile mesh that is tagged. The animation should play where the "Enemy" location is. The script is attached to the Enemy. The game object that has the animation is a 3DText mesh made into a prefab. The animation plays but does not disappear. How can I destroy the prefab (that plays the animation)? If I do: Destroy(gameObject) it will destroy the Enemy only. Do I need to set a clone for the prefab? And destroy the clone? Thanx Guys!

public class Bonus_Animation : MonoBehaviour { //GameObject with the bonus animation attached GameObject spawnBonus;

 //bonus points at 50
 public int points = 50;
     
     
     void OnCollisionEnter (Collision col)
     {
         if(col.transform.tag == "Projectile")
         {
             Instantiate(spawnBonus, transform.position, transform.rotation);
             

                 
         }
     }
     
 }
Comment
Add comment · Show 2
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 Gruffy · Mar 08, 2014 at 11:44 PM 0
Share

I started to answer this and got confused as to what it was exactly you wanted here, so my apologies for that.

Are you saying you would like to instantiate an object on some collision event, and when that happens that you would like it destroyed after some amount of time? (not instantaneously, but a time after instantiation)?. There are quite a few ways to achieve this.. you could implement OnCollisionExit() method to Destroy it when you leave the collision area (denoted by your collider`s volume/area size) when you are trying to destroy something inside your method above you would need to refer to its full path in the scope operation.

If you say Destroy(gameObject); it will destroy the GameObject this code is attached to... To Destroy your instantiated "spawnBonus" gameObject you would have to reach it like so...

 Destroy(spawnBonus.gameObject, 3.0f);

with the 3.0f being an arbitrary time value as a second argument you can give the Destroy() method allowing you to, as a convenient example, play an animation attached to your instantiated object before its destroyed from the game environment.

Hope that helps, but im still not entirely sure of your goals here. probably just me though. Thanks for reading and hope you get it solved. Gruffy

avatar image Bman262 Gruffy · Mar 22, 2016 at 04:47 PM 0
Share

Cheers Gruff ;)

4 Replies

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

Answer by Nick4 · Mar 09, 2014 at 12:12 AM

Yes, you're close. Check this out:

 public GameObject spawnBonus;
 private float time;
 private GameObject instantiatedObj;
 
 void OnCollisionEnter(Collision col)
 {
     if(col.transform.tag == "Projectile")
     {
         instantiatedObj = (GameObject) Instantiate(spawnBonus, transform.position, transform.rotation);
         Destroy(instantiatedObj, time);
     }
 }
Comment
Add comment · Show 11 · 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 PumaManMan · Mar 09, 2014 at 09:08 PM 0
Share

It works...thanx!

avatar image vaibhavatul47 · Oct 09, 2016 at 06:54 PM 0
Share

InvalidCastException: Cannot cast from source type to destination type. Didn't work for me. I got above error.

avatar image cankirici34 · Sep 27, 2020 at 11:05 AM 0
Share

I tried this and some scripts similar but didnt work. like HOW??!

avatar image Nick4 · Sep 27, 2020 at 11:18 AM 0
Share

@cankirici34 share your code, how doesnt it work?

avatar image cankirici34 Nick4 · Sep 27, 2020 at 01:17 PM 0
Share

this is the code and the instantiated prefabs doesn't destroy

    clone = Instantiate(rocks[randomIndex],spawnPoint.position, Quaternion.identity);
         clone.gameObject.tag = "enemy";
        
 
         Rigidbody rockRB = clone.GetComponent<Rigidbody>();
         rockRB.velocity = new Vector3(0, 0, -speed);
         timer = 0f;
 
         if (clone.transform.position.z < 0)
         {
             Destroy(clone);
         }

avatar image Nick4 cankirici34 · Sep 27, 2020 at 01:48 PM 0
Share

hm make sure you have only 1 object named "clone", because if you have another field called clone, it may try to destroy that one instead. And one more thing, make sure from the inspector panel that clone's z position is really below 0. Let me know how it goes.

Show more comments
avatar image
1

Answer by Zebbi · Mar 24, 2021 at 10:57 PM

Late but:

 Destroy(Instantiate(gameobject, position, rotation), time);
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 superluigi · Mar 09, 2014 at 12:04 AM

You can add a script to your animation mesh that can read something like

 var life : float;
 
 function Start()
 {
    life = Time.time + 1.0;
 }
 
 function Update()
 {
    if(life <= Time.time)
    {
       Destroy(gameObject);
    }
 }

not tested but seems like it work. Let me know if it doesn't.

Comment
Add comment · Show 1 · 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 superluigi · Mar 09, 2014 at 12:18 AM 0
Share

Also just to help you understand my answer. At Start() life will equal whatever the current time is plus 1. Then in Update it'll keep checking for when life is smaller than the current time. Since we made life equal the current time plus 1 it'll be bigger then the current time at first. As time passes however it'll eventually become greater than life (in my example it'll take 1 second) and the object will be destroyed.

avatar image
0

Answer by Jahidulh · Aug 17, 2020 at 12:52 PM

public GameObject spawnBonus;

private GameObject spawnBonusClone;

void OnCollisionEnter(Collision col) { if(col.transform.tag == "Projectile") { spawnBonusClone = Instantiate(spawnBonus, transform.position, transform.rotation); Destroy(instantiatedObj, 2.0f); // 2.0 seconds show then destroy } }

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't Destroy Instantiated Prefab 2 Answers

Instantiate Pre-fab clone altered on Instantiate 2 Answers

Can't seem to destroy instantiated objects. 1 Answer

Can a Network.Instantiate()'d object be Object.Destroy()'d? 0 Answers

Checking Instantiate/Destroy has been called | Checking number of scene GameObjects 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