- Home /
Destroying an instantiated prefab particle effect
I have a global inspector variable and I can declare it either this way:
var explosion :GameObject;
or this way
var explosion :Transform;
I then also have an explosion effect made with SHURIKEN tied to my script via the inspector.
When I call on this explosion I instantiate it like this (in the place of my object), to make sure that explosion is there in the inspector
if(explosion)
{
Instantiate(explosion,transform.position,transform.rotation);
}
How do I remove the effect after it has done its job ?
I tried:
if(explosion)
{
Instantiate(explosion,transform.position,transform.rotation);
Destroy(explosion.gameObject,3);
}
and I got:
Destroying assets is not permitted to avoid data loss.
Does anyone have an idea ?
Answer by Jason B · Mar 21, 2012 at 03:03 PM
if(explosion)
{
GameObject newexplosion = (GameObject)Instantiate(explosion,transform.position,transform.rotation);
Destroy(newexplosion,3);
}
You need to store what you instantiated in a temporary variable so that you can talk to it afterwards.
+1. You may not use Destroy(explosion) as the explosion variable points to the actual asset and not an instance of that asset. Store the instance on creation and destroy that later.
Thanks, that did it! $$anonymous$$udos and a cookie to You Sir!
Using GameObject didn't work to me to destroy the explosion. If I cast the Instantiate with "as GameObject", the Destroy won't do anything. If I cast like you said, I get this error using C#:
InvalidCastException: Cannot cast from source type to destination type.
But Instantiating to a Transform, and Destroying the transform.gameObject worked just fine :)
Thanks for this tip.
Thank you, this solve one of my issue :) I'm not sure how the reward works but here's a love heart <3
Answer by Sarnum · Mar 21, 2012 at 03:56 PM
I'm very sorry but I'm using Shuriken particle system and not the old legacy elipsoid particle emitter that had the autodestruct button. If I used the old one I wouldn't have that problem but I do not want to use an outdated system while learning unity 3.5.
Answer by citizen_rafiq · Oct 27, 2012 at 12:27 AM
1.unchecked looping from particle system editor 2.create a transform variable in your script as public(example explosionTrans)and set your particle object from editor. 3.now in your update method explosionTrans.particleSystem.transform.position=this.transform.position; explosionTrans.particleSystem.Play();
Answer by ramp · Jan 28, 2013 at 06:19 AM
if(explosion) { var newexplosion : GameObject = Instantiate(explosion,transform.position,transform.rotation); Destroy(newexplosion,3); }
Your answer
Follow this Question
Related Questions
How many time does a prefab takes to load?? 1 Answer
How to spawn a new prefab when the last one was destroyed?? 2 Answers
Trouble with destroying an instantiated prefab 2 Answers
help for start my project... 2 Answers
Destroying Objects. 1 Answer