C# 带c的Unity3D彩色多个子游戏对象#

C# 带c的Unity3D彩色多个子游戏对象#,c#,unity3d,C#,Unity3d,在我的Unity项目中,我想要多个子游戏对象用红色着色。 在几个论坛上,我找到了用c#实现这一点的解决方案,但不幸的是,我找不到适合我的解决方案 我的所有子对象都有标记“colorChild” 这就是我迄今为止所尝试的: using UnityEngine; public class ColorChildObject : MonoBehaviour { public GamObject PartentColor; public GameObject[] coloredChild

在我的Unity项目中,我想要多个子游戏对象用红色着色。 在几个论坛上,我找到了用c#实现这一点的解决方案,但不幸的是,我找不到适合我的解决方案

我的所有子对象都有标记“colorChild”

这就是我迄今为止所尝试的:

using UnityEngine;

public class ColorChildObject : MonoBehaviour
{
    public GamObject PartentColor;
    public GameObject[] coloredChilds;
    public Material red;

    void Start()
    {
        if(coloredChilds == null)
           // coloredChilds = ParentColor.transform.FindChild("ChildName")
           // coloredChilds = ParentColor.transform.FindChild("colorChild")

           // coloredChilds = GameObject.FindGameObjectswithTag("colorChild")
           // coloredChilds = ParentColor.FindGameObjectswithTag("colorChild") 

        foreach (GameObject colorChild in colorChilds)
        {
            colorChild.GetComponent<Renderer>().material = red;
        }
    }
}
使用UnityEngine;
公共类ColorChildObject:MonoBehavior
{
公共物品颜色;
公共游戏对象[]彩色儿童;
公共材料红色;
void Start()
{
if(coloredChilds==null)
//coloredChilds=ParentColor.transform.FindChild(“ChildName”)
//coloredChilds=ParentColor.transform.FindChild(“colorChild”)
//coloredChilds=GameObject.FindGameObjectswithTag(“colorChild”)
//coloredChilds=ParentColor.FindGameObjectswithTag(“colorChild”)
foreach(colorChilds中的游戏对象colorChild)
{
colorChild.GetComponent().material=red;
}
}
}
不幸的是,这四条评论中没有一条对我有效。我希望有人能告诉我怎么做

非常感谢

请注意

if(coloredChilds == null)
在这里,永远不会成为
真的

是一个序列化字段,因为它是
公共的
,因此由Unity Inspector自动初始化为空数组并存储在场景文件中


或者将其设置为私有的,这样它就不会被序列化了

private GameObject[] coloredChilds;
或者更改支票的长度,例如

if(coloredChilds.Length == 0)
    ...
第一种方法肯定更安全,因为您可以通过Inspector将对象添加到数组中,而Inspector甚至没有
渲染器
组件,结果会出现异常;)


那么前两个甚至都不会编译

coloredChilds = ParentColor.transform.FindChild("ChildName");
coloredChilds = ParentColor.transform.FindChild("colorChild");
因为
FindChild
返回单个引用,并且您希望将其分配给数组。(我不知道您使用的是哪个Unity版本,但它也被删除了,您可能更应该使用
transform.Find

第二个取决于你想要什么

coloredChilds = GameObject.FindGameObjectsWithTag("colorChild");
将在整个场景中搜索这些孩子

coloredChilds = ParentColor.FindGameObjectswWthTag("colorChild");
仅在
ParentColor

两者都只返回活动的游戏对象!在这两种情况下,
W
都应该是大写


事实上,我不会使用你找到的任何解决方案,而是使用并简单地做一些类似的事情

// returns all Renderer references of any children (also nested) of ParentColor
// including the Renderer of ParentColor itself if exists
// by passing true this also returns inactive/disabled Renderers
Renderer[] coloredChildren = ParentColor.GetComponentsInChildren<Renderer>(true);

foreach(var child in coloredChildren)
{ 
    child.material = red; 
}
或者按您已分配的标签进行操作

foreach(var child in coloredChilds)
{
    if(!child.CompareTag("colorChild")) continue;

    child.material = red;
}

听起来很可能在代码运行的时候,孩子们还没有被制作出来..太棒了。。谢谢。我尝试了
GetComponentsinChildren
,第一次就成功了。。你是一个了不起的人,不仅它永远不会是真的,如果它是真的,程序会吐出错误!
foreach(var child in coloredChilds)
{
    if(child.transform.parent != ParentColor.transform) continue;

    child.material = red;
}
foreach(var child in coloredChilds)
{
    if(!child.CompareTag("colorChild")) continue;

    child.material = red;
}