How can I access the particle system of a child object instantiated at runtime?

I am struggling to figure out how to access the variables of a child object instantiated at runtime;

Essentially I have a chest that, when opened, I want to stop emitting particles.

But of course if I turn off the instantiated child, it just turns off the entire particle system. Ideally, I’d like for the system to stop generating particles, so they sort of fade out.

Trying this:

particleFXInstantiated.GetComponentInChildren<ParticleSystem>().startLifetime = 0f;

But when I do this I get a warning that it’s been obsolete or deprecated.

From my research, I’m learning it may not be possible to access the variables of a particle system that was instantiated at runtime. Is that accurate?

Please help! Thanks.

You could stop the particles by calling the particle system’s Stop method:

GetComponent<ParticleSystem>().Stop();

You can access all parameters of the particle system but it’s necessary to first get the relevant module reference like so:

        ParticleSystem ps=GetComponent<ParticleSystem>();
        var main=ps.main;
        main.startLifetime=0;
1 Like

This post highlights the danger of “too much stuff on one line.”

This warning:

is completely unrelated to runtime or getting variables.

The warning is about the .startLifetime on ParticleSystem and has nothing to do with how you got the ParticleSystem.

The warning does not care HOW you got that ParticleSystem reference.

Read the warning carefully and it gives you the replacement method, transacting against the main module of the ParticleSystem. Review the docs there, might even be example code.

Generally speaking, if you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

Putting lots of code on one line DOES NOT make it any faster. That’s not how compiled code works.

The longer your lines of code are, the harder they will be for you to understand them.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

If this is a prefab, you really should make a small adaptor class at the root of it and drag the ParticleSystem in there, such that if you change stuff around, like add a second ParticleSystem into the hierarchy below this somewhere, it doesn’t magically and mysteriously break.

Basically I ended up creating a reference for a new script in the object that pointed to the particle generators, and I instantiated to that reference.

Here was the documentation I had trouble finding: