从游戏对象数组(C#)获取子对象

从游戏对象数组(C#)获取子对象,c#,unity3d,C#,Unity3d,我在从游戏对象数组中获取孩子时遇到了麻烦 这是我的剧本: [SerializeField] GameObject[] CameraScriptsTV; protected override void SetPosition() { CameraScriptsTV = this.gameObject.transform.GetChild(0).gameObject; //here is the error foreach (GameObject TV_CameraScript in Cam

我在从游戏对象数组中获取孩子时遇到了麻烦

这是我的剧本:

[SerializeField]
GameObject[] CameraScriptsTV;

protected override void SetPosition()
{
CameraScriptsTV = this.gameObject.transform.GetChild(0).gameObject; //here is the error
   foreach (GameObject TV_CameraScript in CameraScriptsTV)
      {
         TV_CameraScript.GetComponent<Bloom>().enabled = false;
      }
}
[序列化字段]
GameObject[]CameraScriptsTV;
受保护的覆盖无效设置位置()
{
CameraScriptsTV=this.gameObject.transform.GetChild(0.gameObject;//以下是错误
foreach(CameraScriptsTV中的GameObject TV_CameraScript)
{
TV_CameraScript.GetComponent().enabled=false;
}
}
我要让孩子用上面写的东西

我想做的是这样的,例如:

在此脚本中,我从我的层次结构中的摄影机获取子对象:

CameraScriptsAir_1=this.transform.GetChild(0).GetChild(1).GetChild(0).GetChild(2).GetChild(0).gameObject;
CameraScriptsAir_1.GetComponent().enabled=false;
CameraScriptsAir_1.GetComponent().enabled=false;
所以我在这里得到的是AIR_1(克隆),然后得到脚本
Bloom.cs
DepthOfFieldDeprecated.cs


我希望它是清楚的。

我想你想做的就是这样

protected override void SetPosition()
 { 
 foreach (Transform TV_CameraScript in this.transform)
 { 
         TV_CameraScript.GetComponent<Bloom>().enabled = false;
 } 
}
protected override void SetPosition()
{ 
foreach(在此.Transform中转换TV_CameraScript)
{ 
TV_CameraScript.GetComponent().enabled=false;
} 
}

我不明白你为什么想要一组游戏对象来修改一个组件。

我想你想要做的就是这样

protected override void SetPosition()
 { 
 foreach (Transform TV_CameraScript in this.transform)
 { 
         TV_CameraScript.GetComponent<Bloom>().enabled = false;
 } 
}
protected override void SetPosition()
{ 
foreach(在此.Transform中转换TV_CameraScript)
{ 
TV_CameraScript.GetComponent().enabled=false;
} 
}

我不明白你为什么想要一组游戏对象来修改一个组件。

我这样做就解决了这个问题

CameraScriptsTV[0] = this.transform.GetChild(0).GetChild(1).GetChild(5).GetChild(0).GetChild(0).gameObject;
CameraScriptsTV[1] = this.transform.GetChild(0).GetChild(1).GetChild(6).GetChild(0).GetChild(0).gameObject;
CameraScriptsTV[2] = this.transform.GetChild(0).GetChild(1).GetChild(7).GetChild(0).GetChild(0).gameObject;
而不是像foreach语句那样做。但我仍然不满意,因为这是一个糟糕的做法。我也不知道如何缩短或优化这段代码作为for循环语句

编辑:所以我从那里想出了这个

for(int i=0; i<29; i++)
{
    Tests[i]= this. transform.GetChild(0).GetChild(1).GetChild(i+7).GetChild(0).GetChild(0).gameObject; 
}

for(inti=0;i我这样做解决了这个问题

CameraScriptsTV[0] = this.transform.GetChild(0).GetChild(1).GetChild(5).GetChild(0).GetChild(0).gameObject;
CameraScriptsTV[1] = this.transform.GetChild(0).GetChild(1).GetChild(6).GetChild(0).GetChild(0).gameObject;
CameraScriptsTV[2] = this.transform.GetChild(0).GetChild(1).GetChild(7).GetChild(0).GetChild(0).gameObject;
但我仍然不满意,因为这是一个糟糕的做法。我也不知道如何缩短或优化这段代码作为for循环语句

编辑:所以我从那里想出了这个

for(int i=0; i<29; i++)
{
    Tests[i]= this. transform.GetChild(0).GetChild(1).GetChild(i+7).GetChild(0).GetChild(0).gameObject; 
}

for(int i=0;i好吧,还有另一个快捷方式可以实现这一点,您可以使用GetComponentsInChildren()获取所有Bloom组件

这是解决办法

[SerializeField]
Bloom[] bloomComponents;

protected override void SetPosition()
{
    //Get all Bloom components from child, and store it in global variable
    bloomComponents = this.gameObject.GetComponentsInChildren<Bloom>();

    //Loop to it and disable it
    foreach (Bloom bloom in bloomComponents)
    {
        bloom.enabled = false;
    }
}
如果这显示出一些错误,请告诉我,因为我的办公室笔记本电脑没有unity IDE,我无法测试它


请注意:不要在非常庞大的转换层次结构中使用此选项,因为它将循环遍历所有转换层次结构。

好吧,还有另一个快捷方式可以实现此目的,您可以使用GetComponentsInChildren()获取所有Bloom组件

这是解决办法

[SerializeField]
Bloom[] bloomComponents;

protected override void SetPosition()
{
    //Get all Bloom components from child, and store it in global variable
    bloomComponents = this.gameObject.GetComponentsInChildren<Bloom>();

    //Loop to it and disable it
    foreach (Bloom bloom in bloomComponents)
    {
        bloom.enabled = false;
    }
}
如果这显示出一些错误,请告诉我,因为我的办公室笔记本电脑没有unity IDE,我无法测试它



请注意:不要在非常庞大的转换层次结构中使用此选项,因为它将循环遍历所有转换层次结构。

您遇到了什么错误?
GetChild(0)
返回一个
变换
。获取单个变换的
游戏对象
永远也不会产生任何数组。@Draco18s实际上,这只是我如何获得孩子的一个例子,先生。@Steve无法将
游戏对象转换为游戏对象[]
类似于that@NoobProgrammer然后您遇到了Draco18s描述的问题。请尝试创建一个列表并将游戏对象添加到其中。您遇到了什么错误?
GetChild(0)
返回一个
变换
。获取单个变换的
游戏对象
永远也不会产生任何数组。@Draco18s实际上,这只是我如何获得孩子的一个例子,先生。@Steve无法将
游戏对象转换为游戏对象[]
类似于that@NoobProgrammer然后您遇到了Draco18s描述的问题。请尝试创建一个列表并将游戏对象添加到其中。当前脚本附加到哪个对象?那么为什么附加getChilds()?我使用它而不是GameObject。查找会更快。我将它放在一个数组中,因为它们都是相同的路径。当前脚本附加到哪个对象?那么为什么额外的getChilds()呢?我使用它而不是GameObject。查找速度更快。我把它放在一个数组中,因为它们都是相同的路径。它如何从所有这些组件中找到bloom脚本sir?你能解释一下sir
GameObject.GetComponentsInChildren()吗;
将在游戏对象中找到Bloom的所有脚本类型,因此您不必跟踪
transform.GetChild()
,它将自动完成任务,请参阅参考,这样我就不必在检查器上拖动任何东西。因为我的代码不再拖动它们,它们将自动放置在组件上。它与您的代码做的相同,唯一的区别是它更简单易读。它将拖动(或者您应该称之为“应用”)所有bloom组件都提交给inspector。这取决于您是想用这种方式还是另一种方式,因为在编程中有很多方法可以做同样的事情;)它如何从所有这些组件中找到bloom脚本sir?您能解释一下sir
gameObject.GetComponentsInChildren()吗;
将在游戏对象中找到Bloom的所有脚本类型,因此您不必跟踪
transform.GetChild()
,它将自动完成任务,请参阅参考,这样我就不必在检查器上拖动任何东西。因为我的代码不再拖动它们,它们将自动放置在组件上。它与您的代码做的相同,唯一的区别是它更简单易读。它将拖动(或者您应该称之为“应用”)所有bloom组件都要交给inspector。这取决于您是想用这种方式还是另一种方式,因为在编程中,有很多方法可以进行检查