Skip to content

Instantly share code, notes, and snippets.

@daichi-takezawa
Last active June 18, 2021 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daichi-takezawa/91b0a54802a1169ba89c6425dce0cec3 to your computer and use it in GitHub Desktop.
Save daichi-takezawa/91b0a54802a1169ba89c6425dce0cec3 to your computer and use it in GitHub Desktop.
//自身の子オブジェクトから名前検索で取得(一段階下の子オブジェクトのみ)
GameObject child = transform.Find("Child").gameObject;
//子オブジェクトの順番で取得。最初が0で二番目が1となる。つまり↓は最初の子オブジェクト
GameObject child = transform.GetChild(0).gameObject
//孫(子オブジェクトの子オブジェクト)を取得する。
//以下の場合なら自身の子オブジェクトChildの子オブジェクトGrandChildを取得
GameObject grandChild = transform.Find("Child/GrandChild").gameObject;
//子オブジェクトの全て(子の子の子のオブジェクトとかも)を取得する
void Start {
GetChildren(this.gameObject);
}
void GetChildren(GameObject obj) {
Transform children = obj.GetComponentInChildren < Transform > ();
//子要素がいなければ終了
if (children.childCount == 0) {
return;
}
foreach(Transform ob in children) {
//ここに何かしらの処理
//例 ボーンについてる武器を取得する
//if (ob.name == "Right Hand")
// {
// rightHandWeapon = ob.transform.GetChild(0).gameObject;
// }
GetChildren(ob.gameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment