x


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 ?

more ▼

asked Mar 21 '12 at 01:33 PM

Sarnum gravatar image

Sarnum
3 1 1 2

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first
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.

more ▼

answered Mar 21 '12 at 03:03 PM

Jason B gravatar image

Jason B
1.7k 29 32 44

+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.

Mar 21 '12 at 03:06 PM by0log1c

Thanks, that did it! Kudos and a cookie to You Sir!

Mar 21 '12 at 03:09 PM Sarnum

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.

Sep 11 '12 at 12:27 AM Almondega
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 21 '12 at 03:56 PM

Sarnum gravatar image

Sarnum
3 1 1 2

No worries.

Mar 21 '12 at 07:35 PM fafase
(comments are locked)
10|3000 characters needed characters left

if(explosion) { var newexplosion : GameObject = Instantiate(explosion,transform.position,transform.rotation); Destroy(newexplosion,3); }

more ▼

answered Jan 28 at 06:19 AM

ramp gravatar image

ramp
0 1

(comments are locked)
10|3000 characters needed characters left

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();

more ▼

answered Oct 27 '12 at 12:27 AM

citizen_rafiq gravatar image

citizen_rafiq
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1677
x1259
x764
x164

asked: Mar 21 '12 at 01:33 PM

Seen: 3295 times

Last Updated: Jan 28 at 06:19 AM