所以正如标题所说,这是我的问题。我试过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'。谢谢 –