0

我在父母中有一个 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);
}

在这种情况下,值为part.drawingPoints3 和 4。但它正在停用点GameObject7、8

如果值为part.drawingPoints0,那么它实际上停用了父级,这很奇怪,因为我认为points应该只包含子级

这是GameObject结构,我正在尝试获取Points

Letter-a
--TracingPart
----Part
----Part
--DrawingPoints
----Point
----Point
----Point
----Point
----Point
4

1 回答 1

1

函数 GetComponentsInChildren() 将返回 Transform 类型的所有组件,包括其自身和所有级别的子级。这意味着索引 0 将始终是父变换。如果它的任何一个孩子包含更多的孩子,他们将被依次包含。

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

Transform (0)
--Child (1)
--Child (2)
-----SuChild (3)
--Child (4)

如果要在 Transform 的直接子项中保持索引正确,则应使用 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);
}
于 2019-09-08T10:56:09.093 回答