페이지

2017년 6월 12일 월요일

GetComponentInChildren method can get also parent's Component

GetComponentInChildren,GetComponentsInChildren  methods can get all components in parent and childrens. So if the component that you want to get is in both parent and children, this method can get component in parent at first.


 GameObject <ParticleSystem> : Parent
     GameObject <ParticelSystem> : Child


ParticleSystem ptc = GetComponentInChildren<ParticleSystem>();
Debug.Log(ptc.name);  // "Parent"


solution:

foreach(ParticleSystem ptc in GetComponentsInChildren<ParticleSystem>())

{

     if( ptc.name == "Parent"){

       //do something by parent

     } else if (ptc.name == "Child"){

         //do something by Child

     }

}

*
 i think this method naming is wrong. many people get confused.