Search Unity

Trying to start/stop all particlesystems in a game object

Discussion in 'Scripting' started by Splosions, Sep 15, 2018.

  1. Splosions

    Splosions

    Joined:
    Apr 29, 2017
    Posts:
    30
    I am trying to get all particle systems in a game object and start/stop them at the same time. Rather than listing each system individual I was hoping to get them all in an array, but it isn't working.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FireEffects_OnOff : MonoBehaviour {
    4.     public GameObject R_Hand_Effects;
    5.  
    6.     public void R_Hand_Effects_On() {
    7.         Component[] emiters;
    8.         emiters = R_Hand_Effects.GetComponents(typeof(ParticleSystem));
    9.         foreach (ParticleSystem p_system in emiters) {
    10.             p_system.Play();
    11.             }
    12.         }
    13.  
    14.     public void R_Hand_Effects_Off() {
    15.         Component[] emiters;
    16.         emiters = R_Hand_Effects.GetComponents(typeof(ParticleSystem));
    17.         foreach (ParticleSystem p_system in emiters) {
    18.             p_system.Stop();
    19.             }
    20.         }
    21.     }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. Splosions

    Splosions

    Joined:
    Apr 29, 2017
    Posts:
    30

    Yup!

    Code (CSharp):
    1. public void R_Hand_Effects_Off() {
    2.         ParticleSystem[] particleSystems = R_Hand_Effects.GetComponentsInChildren<ParticleSystem>();
    3.         foreach (ParticleSystem p_system in particleSystems) {
    4.             ParticleSystem.EmissionModule em = p_system.GetComponent<ParticleSystem>().emission;
    5.             em.enabled = false;
    6.             }
    7.         }