Accessing one among several children's particle system

Hi!

My player game object has several dummy children, each one containing a ParticleSystem.
I use them to trigger several effects whenever needed (since attaching several particle systems to one gameobject is impossible I think).

I’m trying to figure out the correct way to do this (c#):

GetComponentInChildren("particle_stars").GetComponent<ParticleSystem>().enabled=true;

I can’t use transform.Find since I have two players that both have the same children, as you can see on the picture.

30461-untitled-1.jpg

I might also be approaching this the wrong way…any suggestions?

Thank you!!

1 Answer

1

You could run a method like so:

private ParticleSystem GetSystem (string systemName){
    Component[] children = GetComponentsInChildren<ParticleSystem>();
    foreach (ParticleSystem childParticleSystem in children){
        if (childParticleSystem.Name == systemName){
            return childParticleSystem;
        }
    }
    return null;
}

This is just written off the fly, there is probably a better way to do it. There are probably also mistakes, but the principle should work.

Thanks! That's how I was thinking of doing it as well but creating an array every time I need to trigger a particle feels a bit heavy-handed...though I don't really know...

If the array is never going to change then code it like this. private Component[] children; void Start (){ children = GetComponentsInChildren<ParticleSystem>(); } private ParticleSystem GetSystem (string systemName){ foreach (ParticleSystem childParticleSystem in children){ if (childParticleSystem.Name == systemName){ return childParticleSystem; } } return null; } You could go one step further and assign specific references to each particle system if you prefer.

That's what I'll do, thank you so much :)