Get children gameobjects array and assign the script inside

Get children gameobjects array

Get references to all gameobjects that are tagged with a particular tag?

Sort an ArrayList of game objects

...

GameObject assetBundle = GameObject.FindGameObjectWithTag("main");    //找到parent gameobject
Transform[] parts = assetBundle.GetComponentsInChildren<Transform>();  //找到所有含有Transform Component的集合,这里不能写<GameObject>
foreach (Transform part in parts)
  {
    if (part.tag == "main")
      continue;
    if (part.gameObject.GetComponent("TearApartControl"))    //通过<Transform>找到每一个chile gameobject
      continue;
    part.gameObject.AddComponent("TearApartControl");    //为每一个child gameobject附上相同的script
  }

...
原文地址:https://www.cnblogs.com/shawnzxx/p/2880813.html