- Home /
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);
}
}
}
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
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);
}
}
InvalidCastException: Cannot cast from source type to destination type.
Didn't work for me. I got above error.
I tried this and some scripts similar but didnt work. like HOW??!
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);
}
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.
Answer by Zebbi · Mar 24, 2021 at 10:57 PM
Late but:
Destroy(Instantiate(gameobject, position, rotation), time);
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.
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.
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 } }
Your answer
Follow this Question
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