所以正如標題所說,這是我的問題。我試過2個不同的解決它的辦法:foreach語句不能對類型爲UnityEngine.GameObject的變量操作
首先是與此代碼:
var children = GetComponentInChildren<GameObject>();
foreach(var child in children)
{
if(child.name == "HealthBar")
{
HealthBar = child;
}
}
這給了我Unknown Resolve Error
上var
內foreach
循環。
二是這樣的:
var children = GetComponentInChildren<GameObject>();
foreach(GameObject child in children)
{
if(child.name == "HealthBar")
{
HealthBar = child;
}
}
這給了我錯誤的稱號。
我該怎麼辦?無處不在我看着如何通過名稱獲得對象內的對象,到處都是通過第一個示例完成的。
'GetComponentInChildren()'返回一個'GameObject'實例?如果只返回一個對象,則不能/不需要循環。我不知道'GetComponentInChildren'是什麼,但也許你的意思是'GetComponentInChildren >()'? –
KMoussa
是的。我沒有注意到有'GetComponentsInChildren'。問題在於'S'。謝謝 –