Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Balvarez · Feb 14, 2016 at 11:03 PM · c#particleschildrenforeach

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;
       }







Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Balvarez · Feb 16, 2016 at 04:03 AM 2
Share

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!

avatar image EmHuynh Balvarez · Feb 16, 2016 at 04:17 AM 1
Share

@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! (:

avatar image
0

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();
}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges