Enabling and disabling child particles
I'm new to scripting C# and am kind of fumbling around. I'm trying to enable and disable all particlesSystems that are children or grandchildren of the player ship. Currently my script only disables/enables the first child. Here is my C# code... Thanks!
ParticleSystem[] parts = gameObject.GetComponentsInChildren<ParticleSystem>();
foreach (ParticleSystem stuff in parts)
{
ParticleSystem.EmissionModule em = GetComponentInChildren<ParticleSystem>().emission;
em.enabled = false;
}
Answer by EmHuynh · Feb 15, 2016 at 05:40 AM
Hello, @Balvarez!
To achieve your goal, we have to loop through all of the game object's children and disable their emission modules. For each child we process, we loop through its children and do the same thing by disabling their emission modules. Basically, we are creating a loop and a nested loop.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Demo : MonoBehaviour
{
ParticleSystem[] childrenParticleSytems;
bool disabledRelevantPSEmissions = false;
// Initialization:
void Start() {
childrenParticleSytems = gameObject.GetComponentsInChildren< ParticleSystem >();
}
void Update()
{
// Process each child's particle system and disable its emission module.
// For each child, we disable all emission modules of its children.
if( !disabledRelevantPSEmissions )
{
foreach( ParticleSystem childPS in childrenParticleSytems )
{
// Get the emission module of the current child particle system [childPS].
ParticleSystem.EmissionModule childPSEmissionModule = childPS.emission;
// Disable the child's emission module.
childPSEmissionModule.enabled = false;
// Get all particle systems from the children of the current child [childPS].
ParticleSystem[] grandchildrenParticleSystems = childPS.GetComponentsInChildren< ParticleSystem >();
foreach( ParticleSystem grandchildPS in grandchildrenParticleSystems ) {
// Get the emission module from the particle system of the current grand child.
ParticleSystem.EmissionModule grandchildPSEmissionModule = grandchildPS.emission;
// Disable the grandchild's mission module.
grandchildPSEmissionModule.enabled = false;
}
}
disabledRelevantPSEmissions = true;
}
}
}
Let me know if there are any mistakes within this script or if there are improvements that I can make. Thanks!
Wow! Thank you so much for your help I appreciate you spending the time to write that out and explain it so well! Thank you!
@Balvarez it's what I love to do. Thanks for asking question and taking your time to learn. You know where to ask if you have any questions! (:
Answer by mbhagat · Jan 26, 2019 at 09:11 AM
If some one is looking to play particle of particular child this code may help too.
ParticleSystem particle ; PlayParticle() { particle = Player.transform.GetChild(ChildIndex).gameObject.GetComponent (); particle.Play();
}
Your answer
Follow this Question
Related Questions
Playing Through a Queue/List of Audio Sources, One at a Time 1 Answer
Need help. Particles make no sense at all 2 Answers
Infinite Time inbetween if statements? 2 Answers
Find child coordinates and pass them further? 0 Answers
Having Player light torches Using Particle System and OnCollisionEnter? 1 Answer