Children 的组件不会被删除,如果只有一个 child
Children's component not being removed if there is only one child
我导入了一些 .obj,它们都有不同数量的 children,有些有 1 个,有些有更多。在我的游戏中,我可以使用鼠标 select 这些 .obj 之一,它会在我 select 编辑的对象周围画出轮廓。
我的 deselection 功能似乎无法正常工作。当试图从其中一个 object 中删除大纲的脚本时,如果 object 只有一个 child,它就不会这样做。 2 children 似乎可以正常工作 every-time,但 1 child 却不行。肯定是进入了child删除循环,我最近调试了一下,但是只进入了一次,而不是objects有2children进入了三次循环。
知道这里的问题是什么吗?
if(selected != null){
Transform[] ts = selected.GetComponentsInChildren<Transform>();
foreach (Transform child in ts){
Destroy(child.gameObject.GetComponent<cakeslice.Outline>());
}
Destroy(selected.GetComponent<cakeslice.Outline>());
selected = null;
}
GetComponentsInChildren
包括 selected
本身的 Transform
以及嵌套的子项!
Returns all components of Type type in the GameObject or any of its children.
The search for components is carried out recursively on child objects, so it includes children of children, and so on.
另请注意,它有一个可选参数 bool
是否包含当前不活动或禁用的组件。
但是为什么你得到 Transform
而不是 GetComponent<cakeslice.Outline>()>
?要销毁所有 cakeslice.Outline>()
个实例,只需执行
var outlines = selected.GetComponentsInChildren<cakeslice.Outline>(true);
foreach(var line in outline)
{
Destroy(line);
}
我导入了一些 .obj,它们都有不同数量的 children,有些有 1 个,有些有更多。在我的游戏中,我可以使用鼠标 select 这些 .obj 之一,它会在我 select 编辑的对象周围画出轮廓。
我的 deselection 功能似乎无法正常工作。当试图从其中一个 object 中删除大纲的脚本时,如果 object 只有一个 child,它就不会这样做。 2 children 似乎可以正常工作 every-time,但 1 child 却不行。肯定是进入了child删除循环,我最近调试了一下,但是只进入了一次,而不是objects有2children进入了三次循环。
知道这里的问题是什么吗?
if(selected != null){
Transform[] ts = selected.GetComponentsInChildren<Transform>();
foreach (Transform child in ts){
Destroy(child.gameObject.GetComponent<cakeslice.Outline>());
}
Destroy(selected.GetComponent<cakeslice.Outline>());
selected = null;
}
GetComponentsInChildren
包括 selected
本身的 Transform
以及嵌套的子项!
Returns all components of Type type in the GameObject or any of its children.
The search for components is carried out recursively on child objects, so it includes children of children, and so on.
另请注意,它有一个可选参数 bool
是否包含当前不活动或禁用的组件。
但是为什么你得到 Transform
而不是 GetComponent<cakeslice.Outline>()>
?要销毁所有 cakeslice.Outline>()
个实例,只需执行
var outlines = selected.GetComponentsInChildren<cakeslice.Outline>(true);
foreach(var line in outline)
{
Destroy(line);
}