【发布时间】:2017-12-18 07:00:37
【问题描述】:
我想遍历一个变换的所有子节点,但出现错误。
这是我想从中获取所有孩子的变量:
public Transform parentToSearch;
然后我在编辑器中将一个 Transform 对象从 Hierarchy 拖到脚本到 parentToSearch。
然后在脚本的后面,我想循环这个 Transform 的所有孩子:
private void OnGUI()
{
if (hasDescription == true && clickForDescription == true)
{
foreach (GameObject child in parentToSearch)
{
if (child.GetComponent<ItemInformation>() != null)
{
ItemInformation iteminformation = child.GetComponent<ItemInformation>();
if (child.name == objectHit)
{
var centeredStyle = GUI.skin.GetStyle("Label");
centeredStyle.alignment = TextAnchor.UpperCenter;
GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 25, 100, 50), iteminformation.description, centeredStyle);
}
}
}
}
}
异常上线:
foreach (GameObject child in parentToSearch)
这是错误:
InvalidCastException:无法从源类型转换到目标 输入
【问题讨论】:
-
非常不清楚您对这段代码的期望...
Transform有其他转换作为子对象 (docs.unity3d.com/ScriptReference/Transform.html)。