Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
1
Question by Agyatket13 · May 26, 2017 at 10:07 AM · c#particlesystemfindparent-childgetcomponentinchildren

How do I access childs with name and getComponent them ?

alt text

I have this Heirarchy where "StrongAquaragia" and "Strong Aquaragia" are gameobjects and model is a 3d model and Strong Aquaragia break/fx are particle systems.

Now I have attached a C# script to the topmost parent "StrongAquaragia". I have to access the particle systems "Strong Aquaragia break" and "------fx" seperately as different particle systems and need to assign it to variables like m_Explosion and m_fx.

I have tried a lot but not getting it. How do I do it ?

untitled.png (1.6 kB)
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

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by danmartelly_caliper · May 26, 2017 at 12:30 PM

You can use Transform.Find as long as the names are unique as they are in your case (https://docs.unity3d.com/ScriptReference/Transform.Find.html). Transform.Find will find the first child object with that name and return it. So you could do something like this:

 m_Explosion = this.transform.Find("Strong Aquaragia break").GetComponent<ParticleSystem>();
 m_fx = = this.transform.Find("Strong Aquaragia fx").GetComponent<ParticleSystem>();
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
avatar image Cherno · May 26, 2017 at 03:02 PM 1
Share

Another good way of doing it! Personally, I'd add some if statements in case the Transforms aren't found, otherwise the user will get NullReference Errors :)

avatar image
4

Answer by Cherno · May 26, 2017 at 10:21 AM

The best way would be to have variables in your c# script holding references to the two ParticleSystems so you can access them directly anytime. GetComponent is expensive and should be avoided if possible, especially if it is used every frame.

To answer your question:

 foreach(ParticleSystem ps in GetComponentsInChildren<ParticleSystem>()) {
      if(ps.gameObject != gameObject && ps.gameObject.name == "Strong Aquaragia break") {
      //do whatever you want with the ...break ParticleSystem
      Debug.Log("Found " + ps.name);
      m_Explosion = ps;
      }
      if(ps.gameObject != gameObject && ps.gameObject.name == "Strong Aquaragia fx") {
           //do whatever you want with the ...fxParticleSystem
           Debug.Log("Found " + ps.name);
           m_fx = ps;
      }
 }
Comment
Add comment · 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
0

Answer by Emiles · May 26, 2017 at 05:06 PM

 public class StrongAquaragia : Behaviour
 {
     // Drag the components in your heirachy onto the inspector properties..
     public ParticleSystem particle1;
     public ParticleSystem particle2;
     void Awake(){
         particle1.m_Explosion = 10000; /// Which ever particle properties you want to change>
     }
 }

Comment
Add comment · 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
0

Answer by MichaelJT · May 26, 2017 at 05:43 PM

Start by declaring the two particle systems. You could probably try renaming them to something simpler too.

 public ParticleSystem break;
 public ParticleSystem fx;
 
 Start()
 {
      break = gameObject.transform.Find("particle_break").GetComponent<ParticleSystem>();
      fx = gameObject.transform.Find("partical_fx").GetComponent<ParticleSystem>();
 }
 

This should work. Of course you can also assign them in the inspector if you want.

They only really need to be public variables if you want to assign them in in the inspector or control them from another script that isn't attached to the root GameObject.

Comment
Add comment · 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

10 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Rotating an object causes it to warp 0 Answers

Detect click on two objects that are on top of each other 1 Answer

Particle System resets when unparenting 2 Answers


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