![](/img/trans.png)
[英]How can I get all gameobjects to check what gameobjects have colliders and what colliders and what gameobjects dont have colliders at all?
[英]How do I grab all sub gameobjects?
有沒有辦法獲取所有子游戲對象? 我正在嘗試GameObject.Find
但這需要名稱為string
。
您可以找到所有的子GameObject,如下所示:
列出一個列表,告訴Unity類型為GameObject。
List<GameObject> list = new List<GameObject>();
然后使用foreach
循環遍歷父GameObject的變換。 請注意,在代碼中,我們利用了Transform具有gameObject
組件這一事實。
foreach(Transform t in transform)
list.Add(t.gameObject);
最簡單的方法可能是遍歷您的gameObject.transform
,它將遍歷每個子Transform (然后可以從中訪問它們的GameObject):
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Example() {
foreach (Transform child in transform) {
child.gameObject.name = "foo";
// or do something else with child.gameObject
}
}
}
另一種方法是使用GetComponentsInChildren ,但是請注意,如果您正在尋找GameObjects ,這還將返回您正在調用的當前對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.