- Home /
Destroy an instantce of a prefab object
Greetings all I have researched this for days. Found many answer to similar questions.
Here is the short of it. Latest version of unity3d free one Doing a 2D shoot ( start from the basic I always say )
I have a fighter and a enemy. all works great. They ram each other they die. Fighter shoots and kills enemy.
But the explosion never go away.
Explosion are Particle systems. They are prefabs.
When the player kills the enemy or both collide: I call this when killing the enemy, top of the call public GameObject explosion; public GameObject playerExplosion;
if ( other.gameObject.name.Contains("enemy") && other.gameObject.GetComponent().hp < 1 ) { Instantiate(explosion, other.gameObject.transform.position, other.gameObject.transform.rotation); explosion.name ="EnemyExplosion";
Destroy( other.gameObject );
Destroy( this.explosion,1 );
}
I have tried Destroy(gameObject); Destroy(gameobject, 1); When I do this unity tells me quote Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy(Object, Single) DestroyOnContact:DestroyEnemy(GameObject) (at Assets/Scripts/DestroyOnContact.cs:63) HealthScript:Damage(Int32) (at Assets/Scripts/HealthScript.cs:64) HealthScript:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/HealthScript.cs:78) end quote
So I try using DestroyImmediate (theObject, true); it tell me to use Destroy.
I have tried Destroy(explosion); same thing unity tells me.
I tried to add the prefab of the explosion to the enemy or player object as a child hoping it would destroy but I cannot figure how to add it as a child.
So how do you in C# get rid of an instance of a prefab?
That's it
Thanks all
Answer by codestage · Jul 25, 2014 at 08:43 PM
GameObject explosionInstance = (GameObject)Instantiate(explosion, other.gameObject.transform.position, other.gameObject.transform.rotation);
Destroy(explosionInstance, 1);
Thanks that worked. Does not make sense to me, bu tit works
more reading to do then....
not sure how to do the vote thingy
If you question is answered, click on the checkmark to the left of the answer to close the question out and give the person who answered the question a bit of $$anonymous$$arma.
They seriously need to rethink how $$anonymous$$arma and the point system works, people game it...
Thanks I gave it the checkmark, cannot do the thumbs up, says I go not have 15 reputation, what ever that is.
Your answer
![](https://koobas.hobune.stream/wayback/20220613155002im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Clone of rigid body prefab destruction if at certain position 3 Answers
Cannot Destroy Instanitaed prefab. 1 Answer
Destroying Instantiated Prefab 1 Answer
Destroying a instantiated prefab? 2 Answers
My prefab isn't getting destroyed 1 Answer