C# Unity使用.GetComponentsInChildren停用错误的子项<;转变>;()

C# Unity使用.GetComponentsInChildren停用错误的子项<;转变>;(),c#,unity3d,C#,Unity3d,我有一个8个子GameObjects在一个父对象中,我试图停用某些子对象,但它正在停用意外的子对象 //Hide current guide points Transform[] points = letters[currentLetterIndex].transform.GetChild(1).GetComponentsInChildren<Transform>(); for (int i = 0; i < part.drawingPoints.Length; i++)

我有一个8个子
GameObjects
在一个父对象中,我试图停用某些子对象,但它正在停用意外的子对象

//Hide current guide points
Transform[] points = letters[currentLetterIndex].transform.GetChild(1).GetComponentsInChildren<Transform>();

for (int i = 0; i < part.drawingPoints.Length; i++)
    Debug.Log(part.drawingPoints[i]);
    points[part.drawingPoints[i]].gameObject.SetActive(false);
}

函数GetComponentsInChildren()将返回类型转换的所有组件,包括其自身和所有级别的子级。这意味着索引0将始终是父变换。如果它的任何子元素包含更多的子元素,它们将按顺序包含

以下是索引在这种情况下的工作方式:

Transform (0)
--Child (1)
--Child (2)
-----SuChild (3)
--Child (4)
如果希望在转换的直接子级中保持索引正确,则应使用GetChild函数

请尝试以下代码:

//Hide current guide points
Transform points = letters[currentLetterIndex].transform.GetChild(1);

for (int i = 0; i < part.drawingPoints.Length; i++)
    Debug.Log(part.drawingPoints[i]);
    points.GetChild(part.drawingPoints[i]).gameObject.SetActive(false);
}
//隐藏当前引导点
转换点=字母[currentLetterIndex].Transform.GetChild(1);
对于(int i=0;i
谢谢,从我看到的情况来看,这是有道理的。在Javascript中,我可以
consol.log
查看结构
Debug.Log
显示很少。我已经更新了我的问题。这给了我一个错误<代码>无法将第一行的UnityEngine.Transform类型转换为UnityEngine.Transform[]我假设点仍然是数组类型,这可能会导致该错误。在第一行,它应该是Transform,而不是Transform[]。
//Hide current guide points
Transform points = letters[currentLetterIndex].transform.GetChild(1);

for (int i = 0; i < part.drawingPoints.Length; i++)
    Debug.Log(part.drawingPoints[i]);
    points.GetChild(part.drawingPoints[i]).gameObject.SetActive(false);
}