Easy way to tell a particle emitter to auto-destruct?

I have it where I create a particle emitter wherever the player touches the screen. The problem is that when I use Destroy with a timer the particle emitter jolts out of view. When I set it to One Shot, it repeats itself if anything gets out of sync and looks very bad.

What I would like to do is set the autodestruct parameter of the particle emitter but everything I try tells me it’s not a member (of the script, the object, unity, etc.)

I am creationg the object by doing:

// I have a particle Emitter set out of view called Poof.  Here I find it.
var poofer = GameObject.Find("Poof");

// I create a copy of the Poof particle system and place it at the position of the object the player touches.
var newpoof = Instantiate(poofer, myObjectHit.transform.position, Quanterion.Identity);

// Here I try to tell it to auto-destruct when it's done emitting.  Obviously I'm doing something wrong here.
newpoof.autodestruct = 1;

I looked at the match code but it is using Destroy on a timer and not auto-destruct. Figuring this out would also help me to figure out how to access member variables of game types.

Any help on what I’m guessing is a simple operation would be much appreciated.

Thanks!

Well, I figured it out. In case anyone else is wondering apparently the Instantiate() function returns a GameObject that you cannot access.

I first had to create a “var tempObject : GameObject” and then assign it via “tempObject = poofer;”. After I did that I was able to call GetComponent() and everything else I’ve come to expect with game objects.

I guess there’s a non-accessible game object type?

There’s no such thing. Instantiate returns an Object, which has no autodestruct property, so you need to typecast properly.

–Eric

Why did I have to create another game object, make the two equal, and then use the new one? What should I have done?

BTW, I realize GameObject doesn’t have an autodestruct property. That’s not what I was trying to do. The object returned from Instantiate() wouldnt even let me do a GetComponent() call on it. That’s the problem I am having.

For example, this does not work:

var poofer = GameObject.Find("Poof"); 

var newpoof = Instantiate(poofer, myObjectHit.transform.position, Quanterion.Identity); 

newpoof.GetComponent(ParticleEmitter);

Why not?

It does work when I do the following:

var poofer = GameObject.Find("Poof"); 

var newpoof = Instantiate(poofer, myObjectHit.transform.position, Quanterion.Identity); 

var tempObject : GameObject;

tempObject = poofer;

tempObject.GetComponent(ParticleEmitter);

Object has no GetComponent function; only a GameObject does that. newpoof needs to be typed as a GameObject.

–Eric

Ok, I see. The problem is I’m a C/C++/C# guy and some of the syntax in the java code is getting me.

Once I did the following it worked without having to create the extra reference, thanks!

var poofer = GameObject.Find("Poof"); 

var newpoof : GameObject;

newpoof = Instantiate(poofer, myObjectHit.transform.position, Quanterion.Identity); 

newpoof.GetComponent(ParticleEmitter);

You should most definitely use C# then. It gives you way more control and you know exactly what is going on without as much hidden magic stuff.

You don’t have to use the “hidden magic” stuff, you know. Just don’t use it, or put “#pragma strict” in your scripts so you’re forced not to. But it’s nice to have the option.

Instead of

var newpoof : GameObject; 

newpoof = Instantiate(poofer, myObjectHit.transform.position, Quanterion.Identity);

it would be more concise to do

var newpoof : GameObject = Instantiate(poofer, myObjectHit.transform.position, Quanterion.Identity);

–Eric

or even

var foo = (Instantiate(…) as GameObject);

or

var foo = (GameObject) Instantiate(…);