Question by 
               ibouchallikht · Feb 11, 2019 at 03:36 AM · 
                parentchildchildrenchild objectparent and child  
              
 
              destroy other child objects when the parent counts more than 1 child
As the title says, I have a gameobject that can carry child objects. But what I want is that the parent can have only one child, not multiple. So to prevent this, I want to destroy all the childs if the parent has one.
By example
 if (childcount => 1)
 {
 destroy other childs;
 }
 
               Thanks
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by tormentoarmagedoom · Feb 12, 2019 at 02:27 PM
Good day.
First, sure you want to destroy? I think its better prevent set the object as a child if the parent have 1.
If yu want to destroy them, then do something like :
 GameObject ChildToPreserve = TheChildYouDontWantToDestroy;
 if (transform.childCount>1)
 {
 foreach (Transform ChildFound in GetComponentsInChildren<Transform>() )
  {
  if ( ChildFound != ChildToPreserve.transform)
    {
    Destroy (ChildFound);
    }
  }
 }
 
               Bye!
Your answer